Find common elements between two vectors and extract data from matrix (vectorized)

Hello,
I understand this is different question than the one answered in
I need to find elements of one small vector into a big vector, and based on that generate a new vector (same size as big vector) using elements of a matrix.
%A is 4x1
A =[1, 6, 5'] %%elements are different
%B is much larger, 6x1
B =[5 5 1 1 1 6 ]'
%A2 is
A2=[101, 501, 601;
102, 502, 602;
103, 503, 603]
% BBB is same dim as B, but using elements from A2
%ANSWER SHOULD BE:
BBB=[501 502 101 102 103 601]'
Using
[~,X]=ismember(B,A) %[~, ~, Xb]=unique(B) brings same result
Does not bring the correct location of columns in A2. Using
A=sort(A)
[~,X]=ismember(B,A)
does bring the correct location of columns in A2.
But how do I extract the rows?

8 Comments

Using [...] does bring the correct location of columns in A2.
How can it other than coincidentally, since the code does not even use A2?
What you have failed to tell us or explain is the result you want. What does based on that generate a new vector (same size as big vector) using elements of a matrix actually mean? Can you give the desired output for your example?
Thanks.
Sorry I was incorrect.
What am looking for is to get array BBB.
I thought doing
[~,X]=ismember(B,A)
X =
2
2
1
1
1
4
would bring the Column location in matrix A2, but the 4 at the end proves me wrong.
I don't see why ismember(B, A) would return anything to do with A2.
Your BBB is A2([4; 5; 1; 2; 3; 7]) and I have no idea why you picked these indices. They're neither in A or B.
Also, both A and B have duplicates. ismember is always going to ignore the duplicates in the second input (whether it's A or B).
Finally, something is not right with your last comment:
A =[1, 6, 5, 5]'
B =[5 5 1 1 1 6 ]'
[~,X]=ismember(B,A)
outputs
X =
3
3
1
1
1
2
Edit 1: The elements of A are different.
If so, then once I A=sort(A) then
[~,X]=ismember(B,A)
X =
2
2
1
1
1
3
X gives me the column in A2, and the number of times the number repeats gives me the number of rows to use from each column. However I cannot seem to vectorize this in order to arrive to BBB
Ok, Now I understand. Can you give an A, B, A2, and desired BBB that are consistent.
Hi, thanks, these are the arrays, desired answer is BBB
%A is 3x1
A =[1, 6, 5'] %%elements are different and all elements of A are in B
%B is much larger, 6x1, elements can repeat
B =[5 5 1 1 1 6 ]'
%A2 is
A2=[101, 501, 601;
102, 502, 602;
103, 503, 603]
% BBB is same dim as B, but using elements from A2
% Desired answer BBB is 6x1
BBB=[501 502 101 102 103 601]'
Why isn't it
BBB = [601 %first element of B is 5, which is 3rd element of A, hence element (3, 1) of A2
602 %5 -> (3, 2)
101 %1 -> (2, 1)
102 %1 -> (2, 2)
103 %1 -> (2, 3)
501 %6 , 2nd element of A, hence (2, 1
]
Hi, thanks. A2 is not transposed. Element (row,col) (3,1) in A2 is 103 and not 601.
You can take A as sorted, say As=sort(A)

Sign in to comment.

 Accepted Answer

A3 = fix(A2.*(10.^-floor(log10(A2))));
[ii,~] = find(squeeze(all(A3==reshape(B,1,1,[]))));
[~,~,c] = unique(ii,'stable');
jj = cell2mat(arrayfun(@(x)(1:x)',accumarray(c,1),'un',0));
out = A2(sub2ind(size(A2),jj,ii));

4 Comments

is there a way to use it in cases where A2 has random numbers?
for example, A2X has no pattern
A2X=[55501, 3501, 44601;
78702, 5022, 2;
98103, 1503, 333]
Answer should be
BBBX=[3501;
5022;
55501;
78702;
98103;
44601]
Yes, if B =[2 2 1 1 1 3]';
solution:
[~,~,c] = unique(B,'stable');
jj = cell2mat(arrayfun(@(x)(1:x)',accumarray(c,1),'un',0));
out = A2(sub2ind(size(A2),jj,B));
Thanks a lot. Leaving the original B, I get ii
[~,ii] = ismember(B,sort(A))
and use the ii in
A2(sub2ind(size(A2),jj,ii));

Sign in to comment.

Asked:

on 4 Feb 2019

Answered:

on 30 Oct 2022

Community Treasure Hunt

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

Start Hunting!