How to construct a customized outer function for 2 vectors?

1 visualización (últimos 30 días)
SC
SC el 10 de Ag. de 2018
Editada: James Tursa el 10 de Ag. de 2018
Hi,
I have 2 vectors, namely A (dim n) and B (dim m). I want to have some customized outer functions to give me an output of m*n matrix. For example,
A=[7,8,9];
B=[2,1,0,-1]';
C=zeros(4,3);
for i=1:4
C(i,:)=A.^B(i);
end
C
But I don't want a for-loop. Instead, I want to vectorize the calculation. Thus I tried this:-
D=exp(B*log(A))
D is better than C, since C needs a for-loop while D doesn't. But D is still not good enough, since it needs a transformation of log() and exp(). How can I get rid of the transformations, and produce the direct outer product of A.^B (or any other customized outer output f(A,B))?
Many thanks!

Respuesta aceptada

James Tursa
James Tursa el 10 de Ag. de 2018
Editada: James Tursa el 10 de Ag. de 2018
Start with a vectorized function handle, e.g.,
f = @(x,y)x.^y
Then use bsxfun, e.g.,
result = bsxfun(f,A,B);
Note that for later versions of MATLAB, this implicit expansion capability is built-in to several MATLAB operators and bsxfun would not be needed.

Más respuestas (0)

Categorías

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

Productos


Versión

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by