How do I implement bsxfun column-wise?

조회 수: 1 (최근 30일)
Fabian Friberg
Fabian Friberg 2019년 5월 16일
댓글: Matt J 2019년 5월 16일
Say I have a 5x5 matrix A and a 1x5 vector B and a function func that takes a column and a scalar as an argument and returns a scalar. I want to take every column in A and every corresponding element in B, put them into func and store the result in a vector without using for-loops. Basically I want to implement this without explicit loops:
for n=1:length(A)
C(n)=func(A(:,n),B(n));
end
Where C is the vector that's returned.

답변 (1개)

James Tursa
James Tursa 2019년 5월 16일
편집: James Tursa 2019년 5월 16일
C = arrayfun(@(i)func(A(:,i),B(i)),1:size(A,2))
This just hides the loops behind arrayfun ... it doesn't eliminate them.
  댓글 수: 2
Fabian Friberg
Fabian Friberg 2019년 5월 16일
Would the CPU compute this paralelly?
Matt J
Matt J 2019년 5월 16일
Easy to find out. Check the Task manager (on Windows).

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by