cell array for loop assignment
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Wang Jack
am 12 Mai 2020
Verschoben: Dyuman Joshi
am 10 Sep. 2025
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 Kommentar
Prateek Rai
am 18 Jun. 2020
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 .
Akzeptierte Antwort
Voss
am 14 Nov. 2023
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);
0 Kommentare
Weitere Antworten (1)
Dyuman Joshi
am 14 Nov. 2023
Verschoben: Dyuman Joshi
am 10 Sep. 2025
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}
0 Kommentare
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!