New to matlab: Checking if the value of cells are the same in two matrices.
2 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
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
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 Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!