in an assignment a(i) = b the number of elements in b and i must be the same

Hi, I have a problem in MATLAB. while finding the index of minimun number of each row of a matrix of size 150*3 and to make 3 groups of them based on their column no, I am facing a problem. My code is (z is the matrix)
k=0;
l=0;
m=0;
for i=1:150
[r(i),c(i)]=find(z==min(min(z(i,:))))
if c(i)==1
k=k+1
else
if c(i)==2
l=l+1
else
if c(i)==3
m=m+1
end
end
end
end
after 9 rows, the error code shown is ""in an assignment a(i) = b the number of elements in b and i must be the same"". what should I do?

Answers (2)

I didn't see the location for the assignment problem (and you didn't supply the line the error occurred on) but...
[~,c]=min(z,[],2); % column of each row minimum in z
nbin=histc(c,1:size(z,2)); % counts for each column
Your code works fine for me when I create a random matrix of 150*3, but it seems to me that the following code would better achieve what you want and might solve your error at the same time:
[~, idx] = min( z, [], 2 );
k = sum( idx == 1 );
l = sum( idx == 2 );
m = sum( idx == 3 );

This question is closed.

Asked:

on 14 Aug 2014

Closed:

on 20 Aug 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!