compare index 1 to index s within loop. index exceed matrix dim

조회 수: 1 (최근 30일)
Leyon
Leyon 2013년 11월 11일
댓글: Walter Roberson 2013년 11월 12일
I have 12 files (M) each with 12 columns to which I want to compare each file columns to another file columns.
for s =1:numel(M)
if all(strcmpi(M(1),M(s))) == 1
M = M(s);
end
end
However, I am getting index exceed matrix dimension. Any help as to how to improve this loop would be nice.
  댓글 수: 4
Walter Roberson
Walter Roberson 2013년 11월 12일
Perhaps you want any() instead of all() ?
Leyon
Leyon 2013년 11월 12일
I need an all because any() says that the two matrices can have specific columns that are equal. I need all the columns to be equal across all the matrices

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

채택된 답변

Walter Roberson
Walter Roberson 2013년 11월 11일
The first time the test succeeds, you are going to replace your array M with the single element M(s) . That leaves M as only a single element long, so the next iteration of the loop attempting to index it by anything other than 1 is going to fail.
  댓글 수: 4
Leyon
Leyon 2013년 11월 12일
As before, how would I be able to compare the columns in the matrices?
Walter Roberson
Walter Roberson 2013년 11월 12일
You are doing an approximation of comparing columns. But when you find a match, you are overwriting the M array and then attempting to continue matching. Perhaps you want something more like,
M_target = M(false); %copies the type but an empty matrix
for s =1:numel(M)
if all(strcmpi(M(1),M(s))) == 1
M_target = M(s);
end
end

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by