Find the location and determine the corresponding

2019-09-05 07:50发布

问题:

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?

回答1:

As a one-liner:

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


回答2:

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.