Vectorized generation of a cell
1 回表示 (過去 30 日間)
古いコメントを表示
x = 1 : a; y = 1 : b; z(x,y) = {randperm(d,2)}; a non-repeating cell is desired. No for loops please.
2 件のコメント
採用された回答
Matt J
2023 年 12 月 22 日
編集済み: Matt J
2023 年 12 月 22 日
There is no way to create or manipulate cells without for-loops. Things like mat2cell, num2cell, etc... all have for-loops inside them. So do things like cellfun, arrayfun, etc...
It is not clear why you would want a cell array. Your data can be stored in an a-by-b-by-2 array.
a=5; b=3; d=7;
I=nchoosek(1:d,2);
I=[I;fliplr(I)];
r=randi(height(I),a*b,1);
z= reshape(I(r,:) ,a,b,2)
11 件のコメント
Matt J
2023 年 12 月 23 日
It must be a cell. If there is no way, then I have to use for loops.
If you don't care about avoiding for-loops anymore, you can take the result of my proposed code and convert it to a cell with num2cell:
a=5; b=3; d=7;
I=nchoosek(1:d,2);
I=[I;fliplr(I)];
r=randi(height(I),a*b,1);
z= reshape( num2cell(I(r,:),2) ,a,b)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!