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(:,:,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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With