Crosk Cool
Crosk Cool

Reputation: 734

Converting a 4d matrix into an array of 3d matrices in matlab

I have a 4d matrix of dimensions (45x66x53x15) and i want to convert it into a array of 3d matrices by 4th dimension and have to add a character to each one of the matrix as well like ( a for mat1 (45x66x53x1), b for mat2 (45x66x53x2), and so on), i guess a structure array like something with a character field and a 3d matrix in each. What is the best and easiest way to do it?

Upvotes: 0

Views: 685

Answers (1)

NLindros
NLindros

Reputation: 1693

One simple way would be to use a for loop

A = rand(2,3,4,5); % Some example data

for idx = 1:size(A,4); % Loop along the 4th dimension
    B.(char(idx + 96)) = A(:,:,:,idx);
end

This gives:

B = 
    a: [2x3x4 double]
    b: [2x3x4 double]
    c: [2x3x4 double]
    d: [2x3x4 double]
    e: [2x3x4 double]

The char(idx + 96) part is just a way to create a a, b, c ... list based on the index. You can easily replace it with your own field name list

Upvotes: 1

Related Questions