How to match two arrays in Matlab
Show older comments
Hi, I want to know if it is possible to match the values of an array (cell by cell) A (:, 1) with an array B (:, 2) so that when calling any array C (:, 1) it gets the values of B (:, 2). Thanks in advance.
A = B = input C =
1 21 22 1
2 10 11 3
3 23 25 6
4 33 30 8
5 32 35 7
6 21 20 5
7 25 27 4
8 24 18 9
9 19 16 10
10 10 33 2
7 Comments
Ameer Hamza
on 19 Mar 2020
Can you give a clear example of variables A and B, and how do you want to match the elements and what will be the output matrix C.
Rik
on 19 Mar 2020
Can you explain how you determined the values in C and their order? I don't see how you could get C, as there are no matches at all between A and the second column of B.
Rik
on 19 Mar 2020
How would you do that by hand? I don't understand how these relate to one another. As long as I don't understand how it works in the first place I can't suggest a solution.
BobH
on 19 Mar 2020
C seems to a selector array. The value in C is an index into A and B -?
Accepted Answer
More Answers (1)
Is this what you are trying to get? Second column of B based on C matching A
A = 1:10;
B = [21 22;10 11;23 25;33 30;32 35;21 20;25 27;24 18;19 16;10 33];
C = [1 3 6 8 7 5 4 9 10 2];
arrayfun(@(X) B( A(X), 2 ), C )
ans =
22 25 20 18 27 35 30 16 33 11
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!