How to sum frequency of the duplicate value in an array

2 views (last 30 days)
I have a n-dimensional array that I want to sum in a specifc dimension according to an index array, where I want to sum repeated indexes.
[new_index,~]=unique(Index,'stable');
new_A=arrayfun(@(x) sum(A(:,. . .,Index==x,. . .,:),n),new_index,'UniformOutput',false);
where n is the applied dimension.
It is doind the job very well, the problem is I need to use this property to false.
'UniformOutput',false
and when using with codegen I get this error:
??? For code generation, UniformOutput must be true.
  3 Comments
JONATHAN SOARES
JONATHAN SOARES on 15 Feb 2021
Edited: JONATHAN SOARES on 15 Feb 2021
What would be a code which does the job?
I made a palliative solution.
[new_index,~]=unique(Index,'stable');
for x = 1:3
new_A(:,x,:,:) = sum(A(:,. . .,Index==new_index(x),. . .,:),n);
end
where n is the applied dimension.
But I would like to not implement with loops
Walter Roberson
Walter Roberson on 15 Feb 2021
arrayfun() is a loop, just a disguised one.
If you are trying to avoid using arrayfun() thinking that using arrayfun() would surely be faster than using an explicit loop, then you would probably be disappointed when you did timing tests: except for some obscure legacy operations, arrayfun() is typically slower than an explicit loop.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!