필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
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?

채택된 답변

Walter Roberson
Walter Roberson 2016년 4월 29일
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?
  댓글 수: 2
pranith kumar
pranith kumar 2016년 4월 29일
편집: Walter Roberson 2016년 4월 30일
Thanks for the reply.
The original code is different and very long. max(areas) is going to change in each loop as per that code. I just gave a small part to show where my error mainly is.
sorry, I should have given the question in better format.
my areas is a row vector.
I=zeros(1,4);
for i=1:4
[ma,I(i)]=max(areas);
areas(I(i))=min(areas);
end
so I am trying to get the Indices of first four Max areas in to I vector. now please give me a code for that please.
Walter Roberson
Walter Roberson 2016년 4월 30일
Your area variable is becoming a 2D array at some point.
Just before that section of code, add
assert(isscalar(areas), 'areas is unexpectedly array %s', str2num(size(areas)));

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Guillaume
Guillaume 2016년 4월 29일
If areas is anything but a vector or scalar, then the return values of max are not scalar. See:
areas = magic(3)
[ma, l] = max(areas)
If you want the max of the whole matrix:
[ma, l(i)] = max(areas(:));
Note that in that case l(i) is the linear index of the max location.
  댓글 수: 1
pranith kumar
pranith kumar 2016년 4월 29일
sorry, I should have given the question in better format. my areas is a ROW VECTOR .
I=zeros(1,4);
for i=1:4
[ma,I(i)]=max(areas);
areas(I(i))=min(areas);
end
so I am trying to get the Indices of first four Max areas in to I vector. now please give me a code for that please.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by