How do I compare to matrices with each other?

조회 수: 1 (최근 30일)
Hamed Nobarani
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
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에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by