Getting a j x k Matrix from j and k-dimensional Vectors Without a For Loop?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Mark Rzewnicki
el 23 de Mzo. de 2020
Editada: per isakson
el 24 de Mzo. de 2020
Suppose I have two column vectors:
J = j x 1 vector
K = k x 1 vector
And I want to create a j x k matrix, W:
W = j x k matrix
where the columns of W are formed by multiplying the corresponding element of K by the vector J.
The for loop to create W looks like this :
% J = j x 1 vector
% K = k x 1 vector
W = zeros(length(J),length(K));
for i = 1:1:length(K)
W(:,i) = K(i)*J;
end
Numerical example:
J = [1 2 3]';
K = [4 5]';
should result in
W =
4 5
8 10
12 15
Is there any way I can avoid the for loop here? Thanks!
2 comentarios
Respuesta aceptada
per isakson
el 23 de Mzo. de 2020
Editada: per isakson
el 24 de Mzo. de 2020
>> J*K'
ans =
4 5
8 10
12 15
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements 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!