필터 지우기
필터 지우기

How to apply a function to an indexed array without loop

조회 수: 10 (최근 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, :));
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.

답변 (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:

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by