MATLAB Answers

# How do I compare to matrices with each other?

1 ビュー (過去 30 日間)
Hamed Nobarani 2019 年 11 月 21 日
コメント済み: Hamed Nobarani 2019 年 11 月 21 日
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 件のコメント

Ridwan Alam 2019 年 11 月 21 日
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 2019 年 11 月 21 日
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.

サインイン to comment.

### 採用された回答

Ridwan Alam 2019 年 11 月 21 日
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 件のコメント

Hamed Nobarani 2019 年 11 月 21 日
Exactly.
Thanks.

サインイン to comment.

### その他の回答 (1 件)

Erivelton Gualter 2019 年 11 月 21 日
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 件のコメント

Hamed Nobarani 2019 年 11 月 21 日
Actually, it is not just the first 1000 rows I want to check all of the rows of the second box.

サインイン to comment.

サインイン してこの質問に回答します。

Translated by