How to apply a function to an indexed array without loop
3 次查看(过去 30 天)
显示 更早的评论
Is there a way to do the following
A = rand(100,2);
c = randi(5,[100,1]);
for i = 1:5
B(i, :) = mean(A(c == i, :));
end
without a loop?
A is my input data, c is some sort of classification and mean could be any function. I tried
A(c == 1:5, :)
but this just gives me an error.
0 个评论
回答(1 个)
Chris Perkins
2018-1-8
Hi Laura,
You might be able to re-structure your problem a bit and use "arrayfun", which applies a function to each value in the given array. See the following documentation link for more information:
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operators and Elementary Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!