You can use the third pin unique
to assign each unique number to a r
unique identifier. Then you can combine all numbers that have the same identifier with a call accumarray
where the key is a unique identifier and the value is the actual value a
for the corresponding key position in this unique array of identifiers. After you have collected all these values, use accumarray
so that you can use these values ββfor each unique value in r
to reference a
and select the maximum element:
%
r = [1;3;5;7;1;3;6;7;9;11;13;16;9;11;13;16];
a = [...];
%
[~,~,id] = unique(r, 'stable');
out = accumarray(id(:), a(:), [], @max);
'stable'
unique
, . , r
, , .
. 16 , a
, . 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
:
out =
0.7195
0.4231
0.2269
0.6848
0.9808
0.4809
0.3921
0.3980
0.7380
, . , a(1)
a(5)
, 0,6965 0,7195 , - 0,7195. , a(2)
a(6)
, 0.2861 0.4231, - 0.4231 ..
, , . accumarray
, a
, . max
, . max
max
max
( , ... Python NumPy, numpy.argmax
), (.. @(x) ...
), .
maxmod
maxmod.m
. :
function p = maxmod(vals, ind)
[~,ii] = max(vals(ind));
p = ind(ii);
, vals
. , , .
accumarray
:
%
r = [1;3;5;7;1;3;6;7;9;11;13;16;9;11;13;16];
a = [...];
%
[~,~,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
, .