Hi MATLAB guys,
I got stuck at some point and really appreciate your help.
Do you know how can I pre-locate the value of X in the following code? X is a matrix and for each i and j its dimentions will change. For example for given i and j, X has a dimention of Ri * Tj.
N = [1 2 3 4 5 6 7 8 9 10];
M = [11 22 33];
X === zeros %%%%pre-locating here
for k = 1 : 10
for m = 1 : N(k)
for i = 1 : 3;
for j = 1: M(i)
X(:,:, j,i,m,k) = sth;
end
end
end
end
Thanks in advance

2 件のコメント

Rik
Rik 2019 年 4 月 10 日
Do you know the size of sth in advance?
Susan
Susan 2019 年 4 月 10 日
編集済み: Susan 2019 年 4 月 10 日
Thanks for your reply. It depends on the i and j and for different i and j the size of this matrix is different. And, yes, I know the sizes in advance.

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

 採用された回答

James Tursa
James Tursa 2019 年 4 月 10 日
編集済み: James Tursa 2019 年 4 月 10 日

0 投票

Numeric arrays must be rectangular. You cannot have different dimensions for different slices. To get that behavior you would need to use something like a cell array to store your different sized variables. E.g.,
X{j,i,m,k} = sth;
But, even though the contents of the cell array elements do not all have to be the same size, the cell array itself must be rectangular. You could still use it in your above scheme as long as it was OK to have a bunch of empty cells in the spots you were not using.

3 件のコメント

Susan
Susan 2019 年 4 月 10 日
Thanks for your reply. I would like to use cell. As you said, since the dimensions of matrix X is changing, the best way to store it is cell array. I just don't know how to do that. Any idea? Thanks
Rik
Rik 2019 年 4 月 10 日
編集済み: Rik 2019 年 4 月 10 日
You can use the code James suggested, and you can do it like this:
N = [1 2 3 4 5 6 7 8 9 10];
M = [11 22 33];
X=cell(max(M(:)),numel(M),max(N(:)),numel(N));
for k = 1 : numel(N)
for m = 1 : N(k)
for i = 1 : numel(M)
for j = 1: M(i)
X{j,i,m,k} = sth;
end
end
end
end
Susan
Susan 2019 年 4 月 10 日
Thank you so much! I appreciate your help.

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

その他の回答 (0 件)

カテゴリ

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

質問済み:

2019 年 4 月 10 日

コメント済み:

2019 年 4 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by