Matrix slicing in matlab

i have a matrix in which i need to slice into elements but in cell array form

回答 (1 件)

Paul
Paul 2021 年 9 月 4 日

0 投票

Use mat2cell
x = rand(4,2); % the matrix
c = mat2cell(x,[2 2],2); % the cell array
% verify
c{1} - x(1:2,:)
ans = 2×2
0 0 0 0
c{2} - x(3:4,:)
ans = 2×2
0 0 0 0

2 件のコメント

Jared Matthias
Jared Matthias 2021 年 9 月 4 日
edit i would want it to work for any size matrix ie a 2x4 matrix would give me 1x2 or a 6x8 matrix would give me a 3x4 cell array
Paul
Paul 2021 年 9 月 4 日
x = rand(6,8);
% assuming row and column dimensions of x are both even
c = mat2cell(x,2*ones(1,size(x,1)/2),2*ones(1,size(x,2)/2))
c = 3×4 cell array
{2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double}
c{2,2} - x(3:4,3:4)
ans = 2×2
0 0 0 0

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

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品

リリース

R2021a

タグ

質問済み:

2021 年 9 月 4 日

編集済み:

2021 年 9 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by