In MATLAB, is there a way to rotate the elements of a array to another dimension, like:
y=[-1,0,1] --> y=[-1; 0; 1] (like transpose)
y=[-1,0,1] --> y(:,:,1)=-1, y(:,:,2)=0, y(:,:,3)=1
y=[-1,0,1] --> y开发者_Python百科(:,:,1,1)=-1, y(:,:,1,2)=0, y(:,:,1,3)=1
I would like to avoid for loops.
You can do these sorts of matrix operations using transposition, the function RESHAPE, or the function PERMUTE. For example:
y = [-1 0 1]; %# Your 1-by-3 sample array
y2 = y.'; %'# Transposing y gives you a 3-by-1 array
y2 = reshape(y,[3 1]); %# This also gives you a 3-by-1 array
y2 = permute(y,[2 1]); %# This also gives you a 3-by-1 array
y3 = reshape(y,[1 1 3]); %# This gives you a 1-by-1-by-3 array
y3 = permute(y,[3 1 2]); %# This also gives you a 1-by-1-by-3 array
y4 = reshape(y,[1 1 1 3]); %# This gives you a 1-by-1-by-1-by-3 array
y4 = permute(y,[4 1 2 3]); %# This also gives you a 1-by-1-by-1-by-3 array
While reshape and permute are more powerful tools, you can solve the given example quite easy with:
y = [-1 0 1];
y2(:,1)=y;
y3(1,1,:)=y;
y4(1,1,1,:)=y;
精彩评论