Array rotation in MATLAB

In MATLAB, is there a way to rotate array elements into another dimension, for example:

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

+4
source share
2 answers

You can perform these types of matrix operations using transpose , the RESHAPE function , or the PERMUTE function. For instance:

 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 
+5
source

While reshape and permute are more powerful tools, you can easily solve this example with:

 y = [-1 0 1]; y2(:,1)=y; y3(1,1,:)=y; y4(1,1,1,:)=y; 
+3
source

Source: https://habr.com/ru/post/1334076/


All Articles