How do I compare to matrices with each other?
2 views (last 30 days)
Show older comments
Hamed Nobarani
on 21 Nov 2019
Commented: Hamed Nobarani
on 21 Nov 2019
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
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?
Accepted Answer
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);
More Answers (1)
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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!