vertical or horizontal array random combination in single array

5 ビュー (過去 30 日間)
eko supriyadi
eko supriyadi 2022 年 6 月 15 日
回答済み: Voss 2022 年 6 月 15 日
Hi all
As the title above, suppose i have matrix A like:
A={[12;13] ;[1,5] ;[5;6;7]; [1,3,5,4]};
i want the matrix A have vertical or horizontal array, look like:
A = 12 13 1 5 5 6 7 1 3 5 4
or
A =
12
13
1
5
5
6
7
1
3
5
4
i know for produce horizontal array using:
[[12;13]' [1,5] [5;6;7]' [1,3,5,4]]
and for vertical array using:
[[12;13] ;[1,5]' ;[5;6;7]; [1,3,5,4]']
but note for this situation i work with big and long array, where has the number ; and , various.
I really appreciate the help. tks

採用された回答

Bruno Luong
Bruno Luong 2022 年 6 月 15 日
編集済み: Bruno Luong 2022 年 6 月 15 日
A={[12;13] ;[1,5] ;[5;6;7]; [1,3,5,4]};
B = cellfun(@(x) x(:), A, 'unif', 0);
C = cat(1,B{:})
C = 11×1
12 13 1 5 5 6 7 1 3 5

その他の回答 (1 件)

Voss
Voss 2022 年 6 月 15 日
A={[12;13] ;[1,5] ;[5;6;7]; [1,3,5,4]};
% make each element of A a row vector
A = cellfun(@(x)x(:).',A,'UniformOutput',false)
A = 4×1 cell array
{[ 12 13]} {[ 1 5]} {[ 5 6 7]} {[1 3 5 4]}
% horizontally concatenate all elements of A
A = [A{:}]
A = 1×11
12 13 1 5 5 6 7 1 3 5 4
% or ...
A={[12;13] ;[1,5] ;[5;6;7]; [1,3,5,4]};
% make each element of A a column vector
A = cellfun(@(x)x(:),A,'UniformOutput',false)
A = 4×1 cell array
{2×1 double} {2×1 double} {3×1 double} {4×1 double}
% vertically concatenate all elements of A
A = vertcat(A{:})
A = 11×1
12 13 1 5 5 6 7 1 3 5

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by