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

邮差的信 提交于 2019-12-12 01: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.



来源:https://stackoverflow.com/questions/11571676/find-the-location-and-determine-the-corresponding-value-of-another-array-having

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!