MATLAB: The next code without for loop display accurately results but when use the loop show error “Assignment has more non-singleton rhs dimensions than non-singleton subscripts”, why ? and What can I do to solve this problem

hi

for i=1:30
b(i,:,:)=nonzeros(A(i,:,:));
bb(i,:,:)=unique(b(i,:,:));
bbb(i)=sum(bb(i,:,:))/length(bb(i,:,:));
end

Best Answer

Also, look up what length() and squeeze() do (you might be surprised). Next, assign some of those things to intermediate variables before sending them into unique(), length() and other functions and see what their sizes are by using THE DEBUGGER
For example:
temp1 = bb(i,:,:);
whos temp1
temp2 = length(temp1);
whos temp2
temp3 = sum(temp1);
whos temp3
bbb(i)= temp3 / temp2;
It's just basic debugging.