Hi,
Say you have three vectors:
a = [1;2;3;4;5;1;3;1;4];
b = [100;200;300;400;500;400;300;200;100];
c = [123;456;221;111;800;1000;10;25;150];
And you use accumarray so that:
maxval = sparse(accumarray(a(:,1),max(b,c),[],@max))
You get:
maxval =
(1,1) 1000
(2,1) 456
(3,1) 300
(4,1) 400
(5,1) 800
Now lets, say I have a whole lot of variables for each subs (something like 2000 each), and I want the average of the top 3 values using the same method. How can I accomplish this? For example, in the same problem, I want my output to be:
The top three values with subs 1 are: 100,400 and 200, so their average is 233.33 and the first row in my sparse matrix is:
maxval =
(1,1) 1000
and so on.
Is it maybe possible to use maxk as a function handle?

 채택된 답변

Walter Roberson
Walter Roberson 2019년 12월 16일

0 개 추천

accumarray(a(:,1), max(b,c), [], @(v) mean(maxk(v,3)), 0, true) %final parameter is sparse flag

댓글 수: 3

Ayman Al-Sukhon
Ayman Al-Sukhon 2019년 12월 17일
Sorry for the late reply, had a few other kinks to work out. This is a very solid answer and exactly what I was looking for. Can you explain the @v to me?
dpb
dpb 2019년 12월 17일
@() is the preamble to define an anonymous function. The v is the dummy argument variable name Walter chose; you'll see it reflected in the argument to maxk().
All the details about anonymous functions is in the documentation under the general subject of functions.
Ayman Al-Sukhon
Ayman Al-Sukhon 2019년 12월 17일
I see, thank you very much dpb!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

dpb
dpb 2019년 12월 16일
편집: dpb 2019년 12월 16일

0 개 추천

Don't see anyway around with accumarray because the VAL parameter must be 1:1 with rows of SUBS; use findgroups/splitapply or varfun (altho latter must be table or timetable).
g=findgroups(a);
mnk=splitapply(@(x) mean(maxk(x,3)),b,g);
yields
>> mnk =
233.3333
200.0000
300.0000
250.0000
500.0000
>>

댓글 수: 7

Walter Roberson
Walter Roberson 2019년 12월 16일
This is not needed. accumarray() with non-default function, uses the subs as a grouping variable, records all of the values with the same value of the grouping variable, and applies the given function to the valaues. (With the default function of sum(), it can take the short-cut of just totaling the values instead of having to record them all in a vector first.)
dpb
dpb 2019년 12월 16일
편집: dpb 2019년 12월 16일
Ah, yeah, good point...got sidetracked to implement the request rather than just solving the problem.
For things needing something other than the sum() as a starting point, however, may need to collect the inputs first .
Sean de Wolski
Sean de Wolski 2019년 12월 16일
Oftentimes this can be worked around by 1:numel(vals) as the vals vector and then indexing into the original data with the index rather than having accumarray using vals directly.
dpb
dpb 2019년 12월 16일
That's good tip, too, Sean. If I follow you, it takes the grouping vector (or its equivalent however it's generated) above to do that lookup though, correct?
If you were going to do something like
idxcells = accumarray(Subs, (1:numel(Subs)).', [], @(v) {v}, {}, true);
so as to get the indices associated with each subs, and then
[r, c, s] = find(idxcells);
mean3 = sparse(r, c, cellfun(@(x) mean(maxk(Values(x),3))), s));
then you might as well use splitapply() with findgroups(), or the accumarray() that I suggested.
dpb
dpb 2019년 12월 16일
What it looked like to me, too, Walter.
Your's works for the specific instance (or anywhere sum is the needed intemediary); however in the followup here I was thinking of the more general cases where the function needs the elements rather than a single result.
Ayman Al-Sukhon
Ayman Al-Sukhon 2019년 12월 17일
Just wanted to thank everyone else for the help. All the suggestions were good, however Walter's got right to the point for me.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

릴리스

R2018b

질문:

2019년 12월 16일

댓글:

2019년 12월 17일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by