Organizing 2D matrices of unequal column length into single 3D array

Hey !
I am trying to arrange my 2D matrix data of unequal coulumn length as a 3D array. I can do it for equal length matrices but requires help for unequal matrices. I used the following code for equal lengths but can anyone help in modifying this code for arranging 2D matrices of unequal length into 3D array. The empty elements may taken as NaN.
% Let Num of 2D arrays
my3Darray=zeros(20,6,x);
for ind=1:x
V=randi(10,20,3);
I=randi(10,20,3);
my2Darray = [I,V];
my3Darray(:,:,ind) = my2Darray;
end

5 Comments

How are the 2D arrays presented in practice? Are they separate variables? Are they stored in a cell array?
I have to ask, because the example given is just generating random 10x40x3 arrays and trying to page-concatenate them. The given example does not have unequal column length issues at all, since the random arrays are identical in size. However, it does have an addressing issue, since you're generating 10x40x3 arrays and you're trying to insert each 3-page array into a single page of the output array.
Clarification is needed.
Actually, I am generating data in Simulink in which a sample [V, I] can be represented as 2D matrix of 20×6 size. A simple 2D matrix I mentioned in above example is created by concatenation of V matrix and I matrix. This results in matrix. If value of x, for example is 5, my3Darray will be of size. I have tested it with minimum working example and it works exactly in the same way. Simplifying the question, I want to create a 3d array with varying number of rows (instead of 20) for specific value of x. i.e. variable row height × 6 × value of x.
I'm sorry. I'm dumb. I didn't see that was randi() and not rand(). I thought those were 3D.
Do you know ahead of time what the maximum number of rows will be?
@Adam Danz, great ! exactly I was looking for this information and it will work for me.
@DGM, thanks for your comments. Infact, I will generate faults in power transmission lines for fault classification study and format the data in form of matrix. The number of rows will be according to duration of faults that I will generate. Fortunately, the answer of @Adam Danz will help me to arrage the data of variable size.

Sign in to comment.

 Accepted Answer

Use padarray to pad your matricies to a standard size. Then you can concatenate them using cat.

More Answers (0)

Categories

Asked:

on 28 Oct 2022

Moved:

on 31 Oct 2022

Community Treasure Hunt

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

Start Hunting!