cell array for loop assignment
3 ビュー (過去 30 日間)
古いコメントを表示
Here is some code
A1 is a 1*4cell array, I want got a 2*4 cell array, but non of those statement can do,
all I got is nesting cell array
anyone any idear?
A2={};
A1={rand(10,2),rand(10,2),rand(10,2),rand(10,2);}
for i=1:4
A2={A2;mat2cell(A{i},[5,5],2)};
A3{i}=mat2cell(A{i},[5,5],2);
A4{:,i}=mat2cell(A{i},[5,5],2);
A5{1,i}=mat2cell(A{i},[5,5],2);
end
A2={};
A={rand(10,2),rand(10,2),rand(10,2),rand(10,2),};
for i=1:4
A2={A2;mat2cell(A{i},[5,5])};
A3{i}=mat2cell(A{i},[5,5]);
A4{:,i}=mat2cell(A{i},[5,5]);
A5{1,i}=mat2cell(A{i},[5,5]);
end
1 件のコメント
Prateek Rai
2020 年 6 月 18 日
As per my understanding, you are having a 1*4 cell array from which you want to get 2*4 cell array
You can do this by the following sample code :
Here is a sample code :
A2=cell(2,4);
A={rand(10,2),rand(10,2),rand(10,2),rand(10,2);}
for i=1:4
A2{1,i}=A{i};
A2{2,i}=[5,5]; % fill the data of your interest in new column
end
You can refer to the following documentation for more details .
回答 (1 件)
Voss
2023 年 11 月 14 日
A = {rand(10,2),rand(10,2),rand(10,2),rand(10,2)}
nA = numel(A);
result = cell(2,nA);
for ii = 1:nA
result(:,ii) = {A{ii}(1:end/2,:); A{ii}(end/2+1:end,:)};
end
disp(result);
1 件のコメント
Dyuman Joshi
2023 年 11 月 14 日
An approach without loops -
A = {rand(10,2),rand(10,2),rand(10,2),rand(10,2)}
B = horzcat(A{:})
C = mat2cell(B, [5 5], [2 2 2 2])
%For verification
C{1}
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!