Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

is there a way to call vectors one by one from matrix other than "for" loop?

1 visualización (últimos 30 días)
hengzh47
hengzh47 el 29 de Mzo. de 2015
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
I have a matrix [a1,a2,a3,a4; b,b,b,b; c,c,c,c], now I want to call each column for a pre-defined function f(a,b,c). There result will be 4x1 vector. I know how to do this with "for" loop, just wondering if there is a way to vectorize the calculation so that don't need to use the loop.

Respuestas (1)

Matt J
Matt J el 29 de Mzo. de 2015
Editada: Matt J el 29 de Mzo. de 2015
Not for a general function f(). There are ways to hide the for-loop using arrayfun(), but that's not genuine vectorization.
Depending on the form of f(), however, you may be able to pass the entire matrix to f() and have it return a 4xN result whose columns are computed in a vectorized way. We would have to see the definition of f() to say more.
  3 comentarios
Matt J
Matt J el 29 de Mzo. de 2015
Editada: Matt J el 29 de Mzo. de 2015
That function does not look like it produces a 4x1 output. It looks like it returns a scalar. Where are a,b,c?
Matt J
Matt J el 29 de Mzo. de 2015
Editada: Matt J el 30 de Mzo. de 2015
In any case, you can certainly do more vectorization within the for-loop, so that matrix-valued y can be handled
[m,n]=size(y);
h=zeros(m,n);
h(1,:)=omega;
for t=1:m %loop over rows of y
last=h(t-1,:);
h(t,:)=omega + alpha * last.*((y(t-1,:)-r)./sqrt(last)-lambda-gamma).^2 ...
+ beta * last;
end

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by