フィルターのクリア

n-dimensional matrix - Store the indexes of the n-th dimension in n-th cell

3 ビュー (過去 30 日間)
dave
dave 2013 年 9 月 12 日
I have a n-dimensional matrix M. Now I'm looking for a way to create a 1xn cell array, where the index vector of the n-th dimension of M is stored in the n-th cell.
So for example: If the size of M were 40x30x50x20, the resulting cell array should look like this:
myCell = {[1:40], [1:30], [1:50], [1:20]};
How would I do this?

採用された回答

fsgeek
fsgeek 2013 年 9 月 12 日
編集済み: fsgeek 2013 年 9 月 12 日
If M is explicitly defined then could you do
vect = size(M); % vect = [40 30 50 20] in your example of M
%{
dim = length(vect); So that we know how many cells there will be.
For your example dim == 4;
%}
myCell = cell(1, dim); % Pre-allocate for speed (thanks for reminding me, Jan!)
for i = 1:dim
myCell{i} = 1:vect(i); % assign a 1 x vect(i) array to each cell
end
The value of myCell after running this code is:
>> myCell
myCell =
[1x40 double] [1x30 double] [1x50 double] [1x20 double]
I hope this helps!
Louis
  2 件のコメント
dave
dave 2013 年 9 月 12 日
Perfect, thanks very much...
Jan
Jan 2013 年 9 月 12 日
And for efficiency a pre-allocation is recommended:
myCell = cell(1, dim);

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by