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

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 Comments

12 matrices each with 12 columns. The result of strcmpi would be a row with 12 values to which "all" will check if there 1s.
Perhaps you want any() instead of all() ?
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

Sign in to comment.

 Accepted Answer

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 Comments

The loop iterates correctly but doesn't stop correctly. Index 12 compares against 11, but it then tries to compares 13 against 12 which there is no 13 and thus "index exceeds matrix" error. How can i gt it to loop correctly or compare the matrices correctly?
Your only defined desired stopping condition is reaching the end of M. If that is the desired stopping condition then you cannot replace M with M(s)
As before, how would I be able to compare the columns in the matrices?
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

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!