Concatenate a variable number of matrices

1 visualización (últimos 30 días)
Yasmin Tamimi
Yasmin Tamimi el 23 de Oct. de 2017
Editada: Stephen23 el 23 de Oct. de 2017
Hi All,
How can I concatenate a variable number of matrices that is decided by the user's input?
Thanks..
  1 comentario
Stephen23
Stephen23 el 23 de Oct. de 2017
Editada: Stephen23 el 23 de Oct. de 2017
This is simple. Ensure that you store all of the matrices in one cell array C, then you can simply do this:
cat(3,C{:})
You can change the dimension to suit your needs. Also you can easily select how many you want to concatenate, by using simple and efficient indexing 1:N:
cat(3,C{1:N})
Note that accessing lots of separate variables inside a loop is very slow, complex and buggy:

Iniciar sesión para comentar.

Respuestas (1)

KL
KL el 23 de Oct. de 2017
Editada: KL el 23 de Oct. de 2017
I suppose your matrices have (in fact, they should be) consistent dimensions, so you could as well store them in ND array.
dummy = rand(3,3,5);
user_choice = 3; %concatenate first 3 matrices
res = reshape(dummy(:,:,1:user_choice),size(dummy,1),size(dummy,2)*user_choice)
Is this what you want?

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by