MATLAB: I have this error “In an assignment A(I) = B, the number of elements in B and I must be the same.”

My code is like
I=zeros(1,5);
for i=1:4
[ma,I(i)]=max(areas);
end
Error: "In an assignment A(I) = B, the number of elements in B and I must be the same." [ma,I(i)]=max(areas); the second argument y in [x,y]=max(a) is a single element and I am trying to send single element only right?

Best Answer

max() of a 2D array results in a vector, not a scalar. The indices of that would not fit in the single location I(i)
You should be asking yourself why you are doing the same thing in every iteration of the loop. max(areas) is not going to change in the loop.
Why are you allocating 5 locations but only looping to 4?