フィルターのクリア

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

1 回表示 (過去 30 日間)
Pyry
Pyry 2012 年 7 月 29 日
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.

回答 (1 件)

E K
E K 2012 年 7 月 29 日
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 件のコメント
E K
E K 2012 年 7 月 29 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by