Find common elements between two vectors and extract data from matrix (vectorized)
Show older comments
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
Guillaume
on 4 Feb 2019
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?
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
Guillaume
on 4 Feb 2019
Ok, Now I understand. Can you give an A, B, A2, and desired BBB that are consistent.
Dave
on 4 Feb 2019
Guillaume
on 4 Feb 2019
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
]
Dave
on 4 Feb 2019
Accepted Answer
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!