Dr. Stanley Raj
Dr. Stanley Raj

Reputation: 87

Find the location and determine the corresponding value of another array having the same location of one array

If

a=[5 8 1 2 6 7 1 4 2 3 7 8];
b=[7 6 3 1 5 4 2 0 1 8 9 4];

then

a1=[1 7 3] 

corresponds to a matrix and d should be [3 4 8]

d is the exact location of the corresponding a value. How do I find this value?

Upvotes: 0

Views: 6372

Answers (2)

Mehrwolf
Mehrwolf

Reputation: 8527

As a one-liner:

arrayfun(@(x) b(find(a == x, 1, 'first')), a1)

Upvotes: 2

Fantastic Mr Fox
Fantastic Mr Fox

Reputation: 33864

Try this:

c = []
for i = 1:length(a1)
    index = find(a == a1(i));
    c = [c, index(1)]
end

d = []
for i = 1:length(c)
    d = [d, b(c(i))]
end

output is [3 4 8]

Hope this helps.

Upvotes: 2

Related Questions