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.

 채택된 답변

Stephen23
Stephen23 2021년 11월 2일
편집: Stephen23 2021년 11월 2일
"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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

2021년 11월 2일

편집:

2021년 11월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by