MATLAB Answers

How do I implement bsxfun column-wise?

4 ビュー (過去 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.

  0 件のコメント

Sign in to comment.

回答 (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).

Sign in to comment.


Translated by