A two dimensional matrix with columns having different length

61 views (last 30 days)
How can I create a two dimensional matrix having different column length? For example, can I assign two column vectors C1 = {1 2 3 4}' and C2 = {1 2 3 4 5}' to a single matrix C?

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 27 Sep 2011
No. You can't. However, you can use cell array.
C1=[1 2 3 4]';
C2=[1 2 3 4 5]';
C={C1,C2};

More Answers (1)

Walter Roberson
Walter Roberson on 27 Sep 2011
You cannot do that with a numerical array, but you can use cell arrays.
C = {C1, C2};
This will, however, be "two dimensional" only in the degenerate sense that one of the dimensions could be the constant 1. C{2} would be the same as C{1,2} which would be the entire column vector C2. You could use C{2}(3) where you probably would have instead preferred to index as C(3,2) (third row, 2nd column).

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!