Is it possible to call a vector(row/column) into a matrix instead of using for loop ?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Rabindra Biswas
el 21 de Jun. de 2018
Editada: Rabindra Biswas
el 23 de Jun. de 2018
I have one 3X3 matrix where each element of the matrix is a function of some variable 'x',
Also, I have another vector 10x1 which contain all the values of 'x'.
I need to have a 3X3 matrix for each value of 'x' such that the resultant matrix becomes 3X3X10 (3D matrix).
I know the method of the 'for loop', but is it possible to get the same answer using vectorization and reshaping.
Code :-
Following is the code :-
if true
phi=0:pi/8:(2*pi);
Rphi=zeros(3,3,length(phi));
for i=1:length(phi)
Rphi(:,:,i)=[cos(phi(i)) sin(phi(i)) 0;-sin(phi(i)) cos(phi(i)) 0;0 0 1];
end
end
What I want is that to use some sort of vectorization instead of 'for' loop. hope it clarifies the question.
1 comentario
Stephen23
el 21 de Jun. de 2018
@Rabindra Biswas: what does " each element of the matrix is a function of some variable" mean? Do you have a function of a scalar x that returns a 3x3 matrix? If so, is the code in that function vectorized in any way? Can you please upload the code by clicking the paperclip function.
Respuesta aceptada
Stephen23
el 21 de Jun. de 2018
Editada: Stephen23
el 21 de Jun. de 2018
If the function fun has been written to accept a 1x1xN vector, then of course your could do this:
fun(reshape(x,1,1,[]))
This depends on how function fun has been written: if function fun only accept a single scalar input then you will have to use a loop (implicitly or explicitly).
7 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices 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!