フィルターのクリア

concatenating elements of an array in a certain manner

2 ビュー (過去 30 日間)
Mnr
Mnr 2015 年 6 月 28 日
コメント済み: Guillaume 2015 年 6 月 29 日
Hello all,
I have a matrix b, with the dimension of NxLxNm. I would like to first consider b(:,:,1) up to b(:,:,j-1) where j ranges from 1 to Nm. In each iteration of j, I would like to create a new cell c with a total dimension of 1XL, an element dimension of Nxj-1, which concatenates the elements of b(:,:,1) to b(:,:,j-1) in a way such that e.g. c{1}=[b(1,1,1), b(1,1,2), ... b(1,1,j-1); b(2,1,1), b(2,1,2),...b(2,1,j-1),....]; c{2}==[b(1,2,1), b(1,2,2), ... b(1,2,j-1); b(2,1,1), b(2,1,2),...b(2,1,j-1),....], and so forth. I would appreciate if somebody can help me please. Thanks.

採用された回答

Matt J
Matt J 2015 年 6 月 28 日
編集済み: Matt J 2015 年 6 月 28 日
c=permute(b(:,:,1:j-1),[1,3,2]);
c=num2cell(c,[1,2]);
c=c(:).';

その他の回答 (1 件)

Guillaume
Guillaume 2015 年 6 月 28 日
編集済み: Guillaume 2015 年 6 月 29 日
For each j, the creation of the cell array c is a simple mat2cell:
c = mat2cell(b(:, :, 1:j), size(b, 1), ones(1, size(b, 2), j)
Or, if you want to remove the singleton column dimension:
c = cellfun(@squeeze, mat2cell(b(:, :, j), size(b, 1), ones(1, size(b, 2), j), 'UniformOutput', false)
Finally, to create all the c in one go:
allc = arrayfun(@(j) mat2cell(b(:, :, 1:j), size(b, 1), ones(1, size(b, 2)), j), 1:size(b, 3), 'UniformOutput', false)
Note: I'm not too sure why you use j-1, since, if j starts at 1, j-1 index is not valid.
  2 件のコメント
Mnr
Mnr 2015 年 6 月 28 日
Thanks for your valuable note! However, I am getting an error when I run the code.
Guillaume
Guillaume 2015 年 6 月 29 日
Fixed now.

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

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by