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

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.

 Respuesta aceptada

Stephen23
Stephen23 el 2 de Nov. de 2021
Editada: Stephen23 el 2 de Nov. de 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

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Productos

Versión

R2020b

Preguntada:

el 2 de Nov. de 2021

Editada:

el 2 de Nov. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by