Convert 2d vector to 4d vector in Matlab

Is it possible to convert a 2-D vector to a 4-D vector in a matrix?

I could create 2D vectors like for example:

A = [[1 2];[3 4]] -->(code segment_1)

      

and 4-D vectors like

A = ones(2,3,4,8)  -->(code segment_2)

      

Is it possible to create a 4D vector, how could we create a 2D vector using code_segment_1? (Let me be more clear, A=[[[1 2],[3 4]],[[5 6],[7 8]],[[9 10],[11 12]]]

creates a 1x12 matrix instead)

I also saw that we are using a function reshape

to convert a k-dimensional matrix to an l-dimensional matrix, where k> l. This is mainly scaling, but can we scale the dimensions?

+3


source to share


2 answers


You can reshape

your 2D matrix into a 4D array if you don't change the number of elements:

 oneD = 1:12; %// creare 1D vector
 twoD = reshape( oneD, [6 2] ); %// reshape into a 2D matrix
 threeD = reshape( oneD, [2 3 2] ); %// now you have a 3D array
 fourD = reshape( oneD, [1 2 3 2] ); %// 4D with a leading singleton dimension

      



From your question, it sounds like you have experience with numpy. Matlab does not have direct creation on the command line of arrays nd for n

> 2, but directly into reshape

any 2D or 3D array is placed at a larger level.

+3


source


If you want to create a 4D array directly (without reshape

), you need to use a slightly more cumbersome syntax, with cat

:



A = cat(4, cat(3, [1 2],[3 4]), cat(3, [5 6],[7 8]), cat(3, [9 10],[11 12]));

      

+1


source







All Articles