How do I implement bsxfun column-wise?

1 visualización (últimos 30 días)
Fabian Friberg
Fabian Friberg el 16 de Mayo de 2019
Comentada: Matt J el 16 de Mayo de 2019
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.

Respuestas (1)

James Tursa
James Tursa el 16 de Mayo de 2019
Editada: James Tursa el 16 de Mayo de 2019
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 comentarios
Fabian Friberg
Fabian Friberg el 16 de Mayo de 2019
Would the CPU compute this paralelly?
Matt J
Matt J el 16 de Mayo de 2019
Easy to find out. Check the Task manager (on Windows).

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by