3
votes

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

I could create 2-D 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 4-D vector like how we could create the 2-D vector using code_segment_1? (Let me be more clear, A=[[[1 2],[3 4]],[[5 6],[7 8]],[[9 10],[11 12]]] is creating a 1x12 matrix instead)

Also I saw we use reshape function to convert a k-Dimensional matrix into l-Dimensional matrix where k > l. This is basically down scaling but can we upscale the dimensions?

2
You need to use the A(:,:,:,k) notation or reshape a 1D or 2D version of the input into 4D. – Divakar
reshape can also "upscale". Just feed it with the appropriate dimensions – Luis Mendo

2 Answers

3
votes

You can reshape your 2D matrix into a 4D array, as long as you do not 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 seems like you have some experience with numpy. Matlab does not have a direct command-line creation of n-d arrays for n>2, but it is straight-forward to reshape any 2D or 3D array into higher-dimensional one.

1
votes

If you want to directly create the 4D array (without reshape) you need to use a slightly more cumbersome syntax, with the cat function:

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