Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array rotation in MATLAB

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.

like image 371
Frank Avatar asked Feb 25 '23 11:02

Frank


1 Answers

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
like image 189
gnovice Avatar answered Mar 04 '23 10:03

gnovice