MATLAB: Hi every body !! the question is how to get the position of the similar elements in same matrix?

elementssimilar

suppose if i have matrix
M=[0 0 0 0 0;0 0 -1 0 0; 0 0 -1 0 -1;0 0 -1 0 -1; -1 -1 0 -1 -1];
i have to compare each row as in..1st row to 2nd row then 1st row to third row, 1st row to 4th one and 1st to 5th one and then again 2nd row to 3rd ans so on….I m trying to compare first and 2nd row row where
ft=[0 0 0 0 0];
st=[0 0 -1 0 0];
now i want that output should be pos=1,2,4,5. Where 1,2,4,,5 are the position of the similar elemnts in ft and st. I have tried many ways but -1 is the barrier here.
M stuck here plz help me out of this…any kind of help will b appreciated. my email id:- titre.rutika25@gmail.com

Best Answer

Basic idea is
>> find(M(1,:)==M(2,:))
ans =
1 2 4 5
>>
Apply above for each row pairwise; note you'll have to save results in a cell array as there won't necessarily be the same length output vector for each row (and could, theoretically, have an empty row as well).
Implementation is a nested loop;
R=size(M,1); % # rows
k=0; % output counter
for i=1:R
for j=i+1:R
k=k+1;
out{k}=find(M(j,:)==M(i,:));
end
end