필터 지우기
필터 지우기

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 CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by