how to concatenate vectors

3 views (last 30 days)
Sam sanati
Sam sanati on 3 May 2015
Edited: Jan on 3 May 2015
Dears,
T is a vector concatenated from T{J} (J=0:4000) which T{1}, T{2}, ... are not same size. How can I concatenate T as a vector?
Regards
  2 Comments
Geoff Hayes
Geoff Hayes on 3 May 2015
Sam - what are the T{k}? Are they scalars, vectors, matrices, strings or ..??
Sam sanati
Sam sanati on 3 May 2015
Sorry, I forgot to mention. they are vectors.

Sign in to comment.

Accepted Answer

Jos (10584)
Jos (10584) on 3 May 2015
If all elements of T are row or column vectors, you can use VERTCAT or HORZCAT, using comma-separated list expansion:
T1 = {[1;2;3],[4],[5;6;7]} % all column vectors
V1 = vertcat(T1{:}) % single column
T2 = {[1 2 3],[4],[5 6 7]} % all row vectors
V2 = horzcat(T2{:}) % single row
For mixed types it will not work directly and you'll have to reshape them first
T3 = {[1;2;3],[4 5],[6 7]} % mixed type
T3c = cellfun(@(x) reshape(x,[],1), T3,'un',0) ; % convert to column vectors first
V3 = vertcat(T3c{:})

More Answers (1)

Jan
Jan on 3 May 2015
Edited: Jan on 3 May 2015
When the size of the elements of T are not matching, you can use: FEX: Cell2Vec

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!