3

In MATLAB, I have a defined cell array C of

size(C) = 1 by 150

Each matrix T of this cell C is of size

size(C{i}) = 8 by 16

I am wondering if there is a way to define a new multidimension (3D) matrix M that is of size 8 by 16 by 150

That is when I write the command size(M) I get 8 by 16 by 150

Thank you! Looking forward for your answers

1 Answer 1

2

If I'm understanding your problem correctly, you have a cell array of 150 cells, and each cell element is 8 x 16, and you wish to stack all of these matrices together in the third dimension so you have a 3D matrix of size 8 x 16 x 150.

It's a simple as:

M = cat(3, C{:});

This syntax may look strange, but it's very valid. The command cat performs concatenation of matrices where the first parameter is the dimension you want to concatenate to... so in your case, that's the third dimension, and the parameters after are the matrices you want to concatenate to make the final matrix.

Doing C{:} creates what is known as a comma-separated list. This is equivalent to typing out the following syntax in MATLAB:

C{1}, C{2}, C{3}, ..., C{150}

Therefore, by doing cat(3, C{:});, what you're really doing is:

cat(3, C{1}, C{2}, C{3}, ..., C{150});

As such, you're taking all of the 150 cells and concatenating them all together in the third dimension. However, instead of having to type out 150 individual cell entries, that is encapsulated by creating a comma-separated list via C{:}.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for taking the time to fully explain your answer. Great work. There are many that take for granted the syntax of C{:} or C(:) without understanding what it's doing.
@Matt Thanks :) Yes it's a peculiarity that not many people bother to know about, but it's a great thing to know once you learn it. A great use of it is for inputting in function parameters. You could have all of your parameters in a cell, then just call the function by doing func(C{:}), rather than having to type in each individual parameter separated by commas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.