分组元素具有相同ID和寻找最大值以及它的位置 [英] Grouping elements with the same ID and finding the maximum value as well as its location

查看:252
本文介绍了分组元素具有相同ID和寻找最大值以及它的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有长度为16第一个的两个向量,研究,例如是:

I have two vectors of length 16. The first one, r, for example is:

r = [1;3;5;7;1;3;6;7;9;11;13;16;9;11;13;16];

研究包含ID的列表。我想收集重复的ID的指数在研究,以便每个组一个ID指数列表。然后我会用这些指标来访问第二个向量 A 并找到最大值事件对指数每个组。

r contains a list of IDs. I want to collect the indices of the duplicate IDs in r so that each group is a list of indices for one ID. I would then use these indices to access a second vector a and find the maximum value incident on the indices for each group.

所以,我想产生使用输出矢量研究 A 这样:

Therefore, I would like to produce an output vector using r and a such that:

max(a(1),a(5)), max(a(2),a(6)), a(3), a(7), max(a(4),a(8)), max(a(9),a(13)), max(a(10),a(14)), max(a(11),a(15)), max(a(12),a(16))

我也想保持最大值的索引。我怎样才能有效地在MATLAB实现这个?

I also want to keep the indices of the maximum values. How can I efficiently implement this in MATLAB?

推荐答案

您可以使用的 唯一 研究一个唯一的ID每一个唯一编号分配。然后,您可以斌所有与 accumarray 调用其中关键是唯一的ID,而值为 A 对的相应位置的实际值关键这个唯一的ID数组中为止。一旦你收集所有这些值,可以使用 accumarray ,这样就可以使用这些值在研究每一个独特的价值参考到 A ,并选择出的最大元素:

You can use the third output of unique to assign each unique number in r a unique ID. You can then bin all of the numbers that share the same ID with an accumarray call where the key is the unique ID and the value is the actual value of a for the corresponding position of the key in this unique ID array. Once you collect all of these values, use accumarray so that you can use these values for each unique value in r to reference into a and select out the maximum element:

%// Define r and a
r = [1;3;5;7;1;3;6;7;9;11;13;16;9;11;13;16];
a = [...];

%// Relevant code
[~,~,id] = unique(r, 'stable');
out = accumarray(id(:), a(:), [], @max);

因为我们希望出现的顺序分配唯一的ID的稳定标志独特是很重要的。不这样做会排序研究值第一个将ID分配之前,这不是我们想要的。

The 'stable' flag in unique is important because we want to assign unique IDs in order of occurrence. Not doing this will sort the values in r first before assigning IDs and that's not what we want.

下面是一个简单的例子。让我设置你的问题产生存储在随机16单元阵列的<​​/ code>您正在尝试最终指数。我们也将建立研究

Here's a quick example. Let me set up your problem with generating a random 16 element array stored in a which you are trying to ultimately index. We'll also set up r:

rng(123);
a = rand(16,1);
r = [1;3;5;7;1;3;6;7;9;11;13;16;9;11;13;16];

这是什么 A 如下:

>> a

a =

    0.6965
    0.2861
    0.2269
    0.5513
    0.7195
    0.4231
    0.9808
    0.6848
    0.4809
    0.3921
    0.3432
    0.7290
    0.4386
    0.0597
    0.3980
    0.7380

通过code运行后,我们得到这样的:

After running through the code, we get this:

out =

    0.7195
    0.4231
    0.2269
    0.6848
    0.9808
    0.4809
    0.3921
    0.3980
    0.7380

您可以验证自己这给正确的结果。具体来说,第一个元素是最大的 A(1) A(5)这分别是0.6896和0.71​​95和最大为0.7195。同样,第二个元素是最大的 A(2) A(6),这是0.2861和0.4231,最大值为0.4231等

You can verify for yourself that this gives the right result. Specifically, the first element is the maximum of a(1) and a(5) which is 0.6965 and 0.7195 respectively, and the maximum is 0.7195. Similarly, the second element is the maximum a(2) and a(6), which is 0.2861 and 0.4231, and the maximum is 0.4231 and so on.

如果这是你的愿望,还记得指数被用来选择出的最大元素,这将是稍微复杂一些。你需要做的就是调用 accumarray 再一次,但值不会被那些但实际索引值来代替。你会使用最大的第二输出来获得选择的值的实际位置。然而,随着最大的性质,我们不能只抓最大的第二个元素没有显式调用两个最大的-output版本(我真的希望有解决这个另一种方式...... Python有在numpy的一个函数调用 numpy.argmax ),这不能在匿名函数正确封装(即 @(x)的... ),所以你将需要创建自定义函数来做到这一点。

If it is your desire to also remember what the indices were used to select out the maximum element, this will be slightly more complicated. What you need to do is call accumarray once again, but the values won't be those of a but the actual index values instead. You'd use the second output of max to get the actual location of the value chosen. However, with the nature of max, we can't just grab the second element of max without explicitly calling the two-output version of max (I really wish there was another way around this... Python has a function in NumPy called numpy.argmax) and this can't be properly encapsulated in an anonymous function (i.e. @(x) ...), so you're going to need to create a custom function to do that.

创建一个名为 maxmod 新功能,并将其保存到一个名为 maxmod.m 。你把这个里面的功能:

Create a new function called maxmod and save it to a file called maxmod.m. You'd put this inside the function:

function p = maxmod(vals, ind)
    [~,ii] = max(vals(ind));
    p = ind(ii);

这发生在一个阵列和一系列指标来访问阵列,名为丘壑。然后,我们会找到最大这些选定的结果,然后返回该指数给了我们最大的。

This takes in an array and a range of indices to access the array, called vals. We'd then find the maximum of these selected results, then return which index gave us the maximum.

之后,你会打电话 accumarray 像这样:

After, you'd call accumarray like so:

%// Define r and a
r = [1;3;5;7;1;3;6;7;9;11;13;16;9;11;13;16];
a = [...];

%// Relevant code
[~,~,id] = unique(r, 'stable');
out = accumarray(id(:), (1:numel(r)).', [], @(x) maxmod(a,x));

这是现在我所得到的:

>> out

out =

     5
     6
     3
     8
     7
     9
    10
    15
    16

如果你看每一个值,这反映了 A 的哪个位置,我们选择了对应最大每组的。

If you look at each value, this reflects which location of a we chose that corresponds to the maximum of each group.

这篇关于分组元素具有相同ID和寻找最大值以及它的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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