Find the location and determine the corresponding

2019-09-05 07:24发布

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?

2条回答
虎瘦雄心在
2楼-- · 2019-09-05 08:05

As a one-liner:

arrayfun(@(x) b(find(a == x, 1, 'first')), a1)
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-09-05 08:08

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.

查看更多
登录 后发表回答