フィルターのクリア

create cell from array with specified size

3 ビュー (過去 30 日間)
Gaetano Pavone
Gaetano Pavone 2023 年 12 月 19 日
編集済み: Voss 2023 年 12 月 19 日
Hello,
Let mymatrix a 24x1 double, how is it possible to obtain a group as specified_sizex1 cell?
For example:
mymatrix=[1:24];
group={[mymatrix(1) mymatrix(2)],...}

採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 12 月 19 日
編集済み: Dyuman Joshi 2023 年 12 月 19 日
Note that the specified size must completely divide the number of rows in the column vector -
mymatrix=[1:24].';
size(mymatrix)
ans = 1×2
24 1
%Total number of elements in the vector
n = numel(mymatrix);
%Specified size
ss1 = 2;
out1 = mat2cell(mymatrix, repelem(n/ss1, 1, ss1), 1)
out1 = 2×1 cell array
{12×1 double} {12×1 double}
%Example 2
ss2 = 4;
out2 = mat2cell(mymatrix, repelem(n/ss2, 1, ss2), 1)
out2 = 4×1 cell array
{6×1 double} {6×1 double} {6×1 double} {6×1 double}

その他の回答 (2 件)

Fangjun Jiang
Fangjun Jiang 2023 年 12 月 19 日
num2cell(mymatrix)
  2 件のコメント
Gaetano Pavone
Gaetano Pavone 2023 年 12 月 19 日
it doesn't work good for a specified_size different from 2
Fangjun Jiang
Fangjun Jiang 2023 年 12 月 19 日
mat2cell(mymatrix,[1],[6, 6, 6, 6])

サインインしてコメントする。


Voss
Voss 2023 年 12 月 19 日
編集済み: Voss 2023 年 12 月 19 日
mymatrix = (1:24).'
mymatrix = 24×1
1 2 3 4 5 6 7 8 9 10
specified_size = 4;
assert(mod(numel(mymatrix),specified_size)==0, ...
'numel(mymatrix) is not a multiple of specified_size')
group = num2cell(reshape(mymatrix,[],specified_size).',2)
group = 4×1 cell array
{[ 1 2 3 4 5 6]} {[ 7 8 9 10 11 12]} {[13 14 15 16 17 18]} {[19 20 21 22 23 24]}

カテゴリ

Help Center および File ExchangeLanguage Fundamentals についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by