Is it possible to call a vector(row/column) into a matrix instead of using for loop ?

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 Commento

@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.

Accedi per commentare.

 Risposta accettata

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 Commenti

I tried , but it throws error , please check the code below
if true
phi=0:pi/8:2*pi;
fun=@(ph) [cos(ph) sin(ph) 1;sin(ph) cos(ph) 0;1 0 0];
fun(reshape(phi,1,1,17))
end
%%-----ERROR------ Error using horzcat Dimensions of arrays being concatenated are not consistent.
Error in @(ph)[cos(ph),sin(ph),1;sin(ph),cos(ph),0;1,0,0]
%------
fun = @(ph) [cos(ph),sin(ph),ones(size(ph)); sin(ph),cos(ph),zeros(size(ph)); ones(size(ph)),zeros(size(ph)),zeros(size(ph))];
phi = 0:pi/8:2*pi;
fun(reshape(phi,1,1,[]))
But the function handle does not seem to be required:
phi = reshape(0:pi/8:2*pi,1,1,[]);
szp = size(phi);
out = [cos(phi),sin(phi),ones(szp); sin(phi),cos(phi),zeros(szp); ones(szp),zeros(szp),zeros(szp)];
@Stephen Cobeldick: Need one more help. I need to take inverse each of the individual matrix and again stored it in the same 3X3X10 format. Please help.
@Rabindra Biswas: if you mean the matrix inverse then this is only defined for a matrix: you will have to use a loop.
Note that the documentation states "It is seldom necessary to form the explicit inverse of a matrix. A frequent misuse of inv arises when solving the system of linear equations Ax = b", and it proceeds to give better (faster, more accurate) alternatives to solving systems of equations. If you are solving systems of equations then I recommend that you follow this advice.
Hi @Stephen Cobeldick: Actually I am not solving a system of equations, what I have is for each value of phi, I have one 3x3 matrix. So, the net matrix is 3X3Xlength(phi). As you suggested earlier, reshaping help me to avoid the 'for loop'.
But now, to calculate the inverse of each 3X3 matrix, again I need to use the 'for loop' to calculate the inverse of each matrix for each value of phi. I thought maybe we could use 'reshape' once again to do the same trick.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Sparse Matrices in Centro assistenza e File Exchange

Prodotti

Release

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by