Error - ??? Improper assignment with rectangular empty matrix - using "for"

Hello,
I am attempting to run a script through several loops. However I keep receiving an error message no matter what I do to try and fix it. Please see below:
A=[1 3 4 2 5];
B=[1 2 3 4 5];
n=length(A);
Ind=ones(1,n);
for i=1:n
Ind(i)=find(A<=min(B));
A(Ind(i))=Inf;
end
Ind
Essentially I want to fill in the array "Ind" with the indices relating A and B. If successful the output from this particular code should be Ind=[1 4 2 3 5]. Unfortunately this error keeps appearing no matter what I try "??? Improper assignment with rectangular empty matrix." I would appreciate any assistance you could provide.
Thanks!

 채택된 답변

Matt Tearle
Matt Tearle 2011년 2월 18일
You can also use MATLAB set functions, under certain assumptions. In particular, if you expect both A & B to have exactly the same elements (but B is a permutation of A), then this will do the job:
if isempty(setdiff(A,B))
[~,Ind] = intersect(A,B)
else
error('A & B have different elements')
end

댓글 수: 2

How would this work for arrays with multiple rows?
How do you mean, exactly? A and B both have n rows and you want to do this row-by-row? Or each row of A against every row of B?

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

추가 답변 (1개)

Robert Cumming
Robert Cumming 2011년 2월 18일

0 개 추천

Have you tried stepping through your code in debug mode?
Objective: You want to find where each B is located in A - correct?
So for each B (not the min) find where it is in A, i.e.
find(A==B(i));
You should do checks to make sure you dont have duplicates (As that will fail in the above case)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

질문:

2011년 2월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by