Accessing data from cells inside of cells
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Thomas Veith
le 3 Juin 2019
Commenté : Star Strider
le 3 Juin 2019
So I have the code below, which solves an ODE 1000 times with random variables at 10 different intervals (tspans). The result I end up with a 1000x1 cell array of 10x1 cell arrays. Each entry of the 10x1 cell arrays represents a 41x2 numeric array, which is the solution for x1 and x2 in each of the ten tspans. What I would like to end up with is a 410x1000 numeric array, where each column represents ten column 2 entries (x2 solutions) from the 41x2 numeric arrays per each 10x1 cell. Is this possible? Thanks in advance.
n = 1000;
result = cell(n,1);
for k=1:n
tspan1=[0 1];A01=rand;P01=rand;g1=rand;p1=rand;B1=rand;
[t1,x1] = ode45(@(t1,x1) [-g1*x1(1) + p1*x1(1); -x1(1)*x1(2)+ B1*x1(2)], tspan1, [A01 P01]);
tspan2=[1 2];A02=rand;P02=rand;g2=rand;p2=rand;B2=rand;
[t2,x2] = ode45(@(t2,x2) [-g2*x2(1) + p2*x2(1); -x2(1)*x2(2)+ B2*x2(2)], tspan2, [A02 P02]);
tspan3=[2 3];A03=rand;P03=rand;g3=rand;p3=rand;B3=rand;
[t3,x3] = ode45(@(t3,x3) [-g3*x3(1) + p3*x3(1); -x3(1)*x3(2)+ B3*x3(2)], tspan3, [A03 P03]);
tspan4=[3 4];A04=rand;P04=rand;g4=rand;p4=rand;B4=rand;
[t4,x4] = ode45(@(t4,x4) [-g4*x4(1) + p4*x4(1); -x4(1)*x4(2)+ B4*x4(2)], tspan4, [A04 P04]);
tspan5=[4 5];A05=rand;P05=rand;g5=rand;p5=rand;B5=rand;
[t5,x5] = ode45(@(t5,x5) [-g5*x5(1) + p5*x5(1); -x5(1)*x5(2)+ B5*x5(2)], tspan5, [A05 P05]);
tspan6=[5 6];A06=rand;P06=rand;g6=rand;p6=rand;B6=rand;
[t6,x6] = ode45(@(t6,x6) [-g6*x6(1) + p6*x6(1); -x6(1)*x6(2)+ B6*x6(2)], tspan6, [A06 P06]);
tspan7=[6 7];A07=rand;P07=rand;g7=rand;p7=rand;B7=rand;
[t7,x7] = ode45(@(t7,x7) [-g7*x7(1) + p7*x7(1); -x7(1)*x7(2)+ B7*x7(2)], tspan7, [A07 P07]);
tspan8=[7 8];A08=rand;P08=rand;g8=rand;p8=rand;B8=rand;
[t8,x8] = ode45(@(t8,x8) [-g8*x8(1) + p8*x8(1); -x8(1)*x8(2)+ B8*x8(2)], tspan8, [A08 P08]);
tspan9=[8 9];A09=rand;P09=rand;g9=rand;p9=rand;B9=rand;
[t9,x9] = ode45(@(t9,x9) [-g9*x9(1) + p9*x9(1); -x9(1)*x9(2)+ B9*x9(2)], tspan9, [A09 P09]);
tspan10=[9 10];A10=rand;P10=rand;g10=rand;p10=rand;B10=rand;
[t10,x10] = ode45(@(t10,x10) [-g10*x10(1) + p10*x10(1); -x10(1)*x10(2)+ B10*x10(2)], tspan10, [A10 P10]);
x={x1;x2;x3;x4;x5;x6;x7;x8;x9;x10};
result{k} = x;
end
0 commentaires
Réponse acceptée
Star Strider
le 3 Juin 2019
There is no guarantee that the row lengths of each ‘x’ are going to be the same using a two-element ‘tspan’ vector, so what you outlined may not be possible.
However if you fix the number of elements in ‘tspan’, what you want to do is straightforward:
n = 1000;
tsv = linspace(0, 1, 50);
xtm = zeros(numel(tsv)*10,2,n);
for k1 = 1:n
xim = zeros(9*numel(tsv), 2);
for k2 = 1:10
tspan=tsv+(k2-1);
A=rand; P=rand; g=rand; p=rand; B=rand;
[t,x] = ode45(@(t,x) [-g*x(1) + p*x(1); -x(1)*x(2)+ B*x(2)], tspan, [A P]);
xim((1:numel(tsv))+numel(tsv)*(k2-1),:) = x;
end
xtm(:,:,k1) = xim;
end
The ‘xtm’ matrix will be (500x2x1000), representing the concatenated rows, the two columns, and the 1000 trials. It is also easy to index into.
2 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!