MATLAB Answers

How do I compare to matrices with each other?

1 view (last 30 days)
Hi Everyone,
I have two different matrices with diffrent sizes. for example one of them is 1000*3 and the other one is 5000*3.
How can I select/compare the line (I mean 1000 rows of these 5000 rows will be the same in these two matrices) and keep it in another matrice?
box1=cut2d(:,2:4);
box2=rombohedral(:,1:3);
if box1==box2
rombohedral2d=rombohedral;
end
The if part is not working.

  2 Comments

Ridwan Alam
Ridwan Alam on 21 Nov 2019
So ... box1 is 1000x3 and box2 is 5000x3?
When you said "1000 rows of these 5000 rows will be the same in these two matrices", did you mean "consecutive" rows?
Hamed Nobarani
Hamed Nobarani on 21 Nov 2019
Yes for the first question and I will hive you an example for second one:
Box 1 : 1 2 3
4 5 6
Box 2 : . . .
1 2 3
. . .
4 5 6
So now I want to just keep the rows of box 2 which it has the same number in three columns in box 1. Because in box 2 I have more information and I want just too keep some of them.

Sign in to comment.

Accepted Answer

Ridwan Alam
Ridwan Alam on 21 Nov 2019
box1=cut2d(:,2:4);
box2=rombohedral(:,1:3);
[index1,index2] = ismember(box1,box2,'rows');
% index2 holds the indices of the rows that are also in box1
rombohedral2d=rombohedral(index2,:); % I assume you want all the columns, not only 3, right?
% if you want only those 3 columns, use:
% rombohedral2d=rombohedral(index2,1:3);

  1 Comment

Sign in to comment.

More Answers (1)

Erivelton Gualter
Erivelton Gualter on 21 Nov 2019
If I understood right, you want to compare the first 1000 rows of these two matrix.
% Sample Matrix
A = rand(1000, 3);
B = rand(5000, 3);
% C contains 0 and 1 as a results of comparing A and B. Since A and B were rand created,
% matrix C will probably contains only zeros (1000x3)
C = A == B(1:1000,:)
% As a example, lets chage the first rows for each matrix:
A(1,:) = [0 1 2];
B(1,:) = [0 1 5]
C = A == B(1:1000,:)
% The result will be
% C = [1 1 0;
% 0 0 0 .....
It may be not what you want, but can give you some insigth.

  1 Comment

Hamed Nobarani
Hamed Nobarani on 21 Nov 2019
Actually, it is not just the first 1000 rows I want to check all of the rows of the second box.

Sign in to comment.

Sign in to answer this question.