Concatenating Matrices with different columns

Hi, Is there an easy way to concatenate matrices with different columns? That is, if A is 2x3 and B is 2x5, how do I make C=A;B? The problem is sometimes columns in A is larger than columns in B and vice versa.
Thanks.

Respuestas (2)

Youssef  Khmou
Youssef Khmou el 31 de En. de 2014
Editada: Youssef Khmou el 31 de En. de 2014
Linden,
One of the ways is to use cells, here is an example,
A=rand(2,3);
B=rand(2,5);
C=cell(2,1);
C{1,1}=A;
C{2,1}=B;
Walter Roberson
Walter Roberson el 31 de En. de 2014

0 votos

Numeric matrices cannot have varying number of columns. Cell matrices can contain multiple objects that do not need to be the same size as each other.
You can pad matrices as you put them together: compare their size() to figure out how the padding needs to happen.

3 comentarios

Youssef  Khmou
Youssef Khmou el 31 de En. de 2014
Walter, if one needs some measures relative to the concatenated matrices, then the second approach you proposed will introduce external zeros, that will influence the calculus, what do you think?
Walter Roberson
Walter Roberson el 31 de En. de 2014
You can fill with numbers other than zeros, including NaN. NaN would allow you to unambiguously detect padding without danger that actual values to calculate with might happen to be NaN, which would potentially be a problem with padding with 0.
Youssef  Khmou
Youssef Khmou el 31 de En. de 2014
ok

Iniciar sesión para comentar.

Categorías

Preguntada:

el 31 de En. de 2014

Comentada:

el 31 de En. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by