How do I compare to matrices with each other?

2 次查看(过去 30 天)
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
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
Hamed Nobarani 2019-11-21
编辑: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.

请先登录,再进行评论。

采纳的回答

Ridwan Alam
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 个)

Erivelton Gualter
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
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by