Turning 2D array (58x23) into 4D array (58x1x23x1)

11 views (last 30 days)
I would like to turn a 2D array of size 58x23 into a 4D array of size 58x1x23x1. I assume that the answer is rather simple using MATLAB's cat and permute functions. I need this specific format in order to use Simulink's LPV System.
A = rand(58,23);
A2 = cat(4, A, A); % array of size [58 23 1 2]
A3 = permute(A2, [1 3 2 4]); % array of size [58 1 23]
I can't get any further than the above dimensions and would appreciate any help.

Accepted Answer

Stephen23
Stephen23 on 2 Nov 2021
Edited: Stephen23 on 2 Nov 2021
"I can't get any further than the above dimensions"
It is not clear what the problem is: are you expecting trailing singleton dimensions to be explicitly listed when you call SIZE on that array? (they aren't, as for obvious reasons all infinite implicit trailing singleton dimensions are not output by default (but they can be explicitly requested using SIZE's DIM option), nor are they given in infinitely long text in the command window, in the variable viewer, or anywhere else that array sizes are displayed).
Your starting array has 58x23 = 1334 elements, the final array also has 58x1x23x1 = 1334 elements... so why do you concatenate two copies of the array together to get 2668 elements?
Here are two basic approaches:
A = rand(58,23);
B = permute(A,[1,3,2]);
size(B,4) % yep, 4th dimension has size 1 (as do all infinite trailing dimensions)
ans = 1
B = reshape(A,[58,1,23]);
size(B,4) % yep, 4th dimension has size 1.
ans = 1

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!