How to apply a function to an indexed array without loop

24 ビュー (過去 30 日間)
Laura Luidolt
Laura Luidolt 2018 年 1 月 4 日
回答済み: Chris Perkins 2018 年 1 月 8 日
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, :));
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.

回答 (1 件)

Chris Perkins
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:


Find more on Loops and Conditional Statements in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by