New to matlab: Checking if the value of cells are the same in two matrices.

1 view (last 30 days)
I have these two matrices M2(7344x2) and M3(8740x2)
I have this simple for loop that works if all the values in M2(:,1) and M3(:,1) are unique, but now this is not the case. Values repeat themselves 24 times in both matrices.For example M2(1:24,1)=1, M2(25:48,1)=2 and so on.
This is the code that works if the values are unique, how should i modify it?
O2 = M3;
for i=1:size(M2,1)
for j=1:size(M3,1)
if M3(j,1) == M2(i,1);
O2(j,:) = M2(i,:);
end
end
end
end
Thanks in advance!
Edit: So if the value is same, it should replace the whole row with values from M2, but somehow this does not work.

Answers (1)

E K
E K on 29 Jul 2012
I think I can not understand you completely but if you want a matrix without repeating elements you can use
a=unique(matrix_name)
if you are checking for rows
a=unique(matrix_name,'row')
  1 Comment
E K
E K on 29 Jul 2012
ok then. You should use eq() operator to get another matrix which will have boolean values in it. 1 for same element 0 for different.
sorting matrixes which will be compared will increase the speed.
you should also need a for loop for compression.

Sign in to comment.

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!