how to concatenate vectors
3 views (last 30 days)
Show older comments
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
on 3 May 2015
Sam - what are the T{k}? Are they scalars, vectors, matrices, strings or ..??
Accepted Answer
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{:})
See Also
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!