how to find common elements of two matrices considering also the repeated values?

조회 수: 5 (최근 30일)
Hello everybody, i would like to know how to find the position of elements of a matrix who are in another matrix. For example, letting i have two matrix A and B:
A= [2 1 2
3 2 5
4 7 5
4 8 10]
B= [4 7 5
2 8 10
4 1 2
3 2 5]
I would like the output to provide the position of the elements of the B matrix that are present in the A matrix considering also repeated values.
For example "it finds in which position the element B (1,1) is present in the matrix A (:, 1), in which position the element B (2,1) is present in the matrix A (:, 1), in which position the element B (3,1) is present in the matrix A (:, 1) and so on ". So similarly for the other columns "find in which position the element B (1,2) is present in the matrix A (:, 2) etc"
Regarding the repetition I would like to make sure that if, for example, there are 3 repetitions in a column, each repetition will have an increasing order index, ie:
A= [ 2
4
4
4]
B=[4
2
4
4]
output=[2
1
3
4 ]
I tried to use the MATLAB function "intersect", but it give as outputs only the indexes of the columns whose values are not repeated.
for n=1:size(A,2)
[C, idx1, idx2] = intersect( B(:,n), A(:,n), 'stable');
end
I do not know if I've been clear. Sorry.

채택된 답변

Stephen23
Stephen23 2018년 6월 13일
편집: Stephen23 2018년 6월 13일
This is easy with sort, assuming that both A and B contain exactly the same elements:
>> A = [2;4;4;4];
>> B = [4;2;4;4];
>> [~,ida] = sort(A);
>> [~,idb] = sort(B);
>> [~,ida] = sort(ida);
>> [~,idb] = sort(idb);
>> ida(idb)
ans =
2
1
3
4
EDIT: use a simple loop to do matrices of any size. This is simpler because C is preallocated so we can index into it using idb:
A = [2,1,2;3,2,5;4,7,5;4,8,10];
B = [4,7,5;2,8,10;4,1,2;3,2,5];
C = nan(size(A));
for k = 1:size(A,2)
[~,ida] = sort(A(:,k));
[~,idb] = sort(B(:,k));
C(idb,k) = ida;
end
And checking the output:
>> C % my code
C =
3 3 2
1 4 4
4 1 1
2 2 3
>> [3 3 2; 1 4 4; 4 1 1; 2 2 3] % requested output
ans =
3 3 2
1 4 4
4 1 1
2 2 3
  댓글 수: 7
Stephen23
Stephen23 2018년 6월 13일
편집: Stephen23 2018년 6월 13일
@Gessica Cos: see my edited answer. Neither ismember nor interesect will do what you want.
Gessica Cos
Gessica Cos 2018년 6월 14일
Thanks so much. You've been really kind. You have been very helpful to me.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
Image Analyst 2018년 6월 13일
Try ismember().
  댓글 수: 1
Gessica Cos
Gessica Cos 2018년 6월 13일
I tried to use "ismember" but I have not succeeded.Could you give me an idea of the code I should write?

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2018년 6월 13일
Try this:
A= [2 1 2
3 2 5
4 7 5
4 8 10]
B= [4 7 5
2 8 10
4 1 2
3 2 5]
% Find unique numbers in each of the two arrays.
ua = unique(A)
ub = unique(B)
% Check each number in B to see where it lives in A.
for k = 1 : length(ub)
% Get this number from B
thisB = ub(k);
% Find out what rows and columns it shows up at in A
[rows, columns] = find(A == thisB);
% Save the number we were looking for into column 1 of the cell array.
locations{k, 1} = thisB;
% Save the locations that number was found into column 2 of the cell array.
locations{k, 2} = [rows, columns];
end
celldisp(locations)
Be sure to read the FAQ https://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F so you understand what I did.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by