如何找到与其他向量的最大值对应的向量的索引? [英] How to find the index of vector that corresponds with highest value of other vector?

查看:141
本文介绍了如何找到与其他向量的最大值对应的向量的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个向量,例如

A=[0.2 0.5 0.4 0.6] 

标记为

A_labels=[1 2 3 4]

其他向量B等于

B=[30 10 20]

我假设向量B的最大值将分配给A中的最高标签,并按顺序减小.那是

30 will assign for 4
10 will assign for 2
20 will assign for 3

我将扫描向量B的所有元素,并希望根据上述规则找到与之对应的标签.您能帮我在MATLAB中实现该方案吗?谢谢

A=[0.2 0.5 0.4 0.6]
A_lables=1:1:size(A,2);
B=[30 10 20];
for i=1:size(B,2)
    //Find label of A_labels corresponds with B(i)
    // Result will be [4 2 3]
end

解决方案

我认为这可以满足您的需求.如您的示例所示,我假设对A_labels进行了排序.

[~, ind] = sort(B); %// sort B and get *indices* of the sorting
[~, ind] = sort(ind); %// get *rank* of each element of B
result = A_labels(end-numel(ind)+ind);

I have a vector such as

A=[0.2 0.5 0.4 0.6] 

that labels as the

A_labels=[1 2 3 4]

and other vector B equals

B=[30 10 20]

I assume that the highest values of vector B will be assigned for highest label in A, and reduces by order. That means

30 will assign for 4
10 will assign for 2
20 will assign for 3

I will scan all element of vector B and I want to find which labels corresponding with its based on above rule. Could you help me implement that scheme in MATLAB? Thanks

A=[0.2 0.5 0.4 0.6]
A_lables=1:1:size(A,2);
B=[30 10 20];
for i=1:size(B,2)
    //Find label of A_labels corresponds with B(i)
    // Result will be [4 2 3]
end

解决方案

I think this does what you want. I'm assuming A_labels is sorted, as in your example.

[~, ind] = sort(B); %// sort B and get *indices* of the sorting
[~, ind] = sort(ind); %// get *rank* of each element of B
result = A_labels(end-numel(ind)+ind);

这篇关于如何找到与其他向量的最大值对应的向量的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆