Creating a matrix per iteration

6 vues (au cours des 30 derniers jours)
Martin Matin
Martin Matin le 6 Mar 2015
Commenté : Geoff Hayes le 8 Mar 2015
Hello,
I'm trying to create a new matrix for each iteration of my loop. Here's the problem, I have to test 3 differential equations solvers, i.e. ode45, ode23, ode15s. I'd like to save the result of each loop in a matrix.
Here's the code :
for i=1:5
for j=1:5
options(i,j) = odeset ('RelTol',10^(-i),'AbsTol',10^(-j));
[T1,X]=ode45(@odefunction,[0 5],[0 5],options(i,j));
[T2,Y]=ode23(@odefunction,[0 5],[0 5],options(i,j));
[T3,Z]=ode15s(@odefunction,[0 5],[0 5],options(i,j));
end
I don't know the size of the matrix since the result of each ode gives a different size.
Thank you.

Réponse acceptée

Geoff Hayes
Geoff Hayes le 7 Mar 2015
Martin - save your output to a cell array which allows for each element to be of a different type or size which is perfect for your example. For example, you could have one cell array where each element is a structure that has fields for X, Y, Z, T1, T2, and T3. Something like
data = cell(5,5);
for i=1:5
for j=1:5
options(i,j) = odeset ('RelTol',10^(-i),'AbsTol',10^(-j));
[T1,X]=ode45(@odefunction,[0 5],[0 5],options(i,j));
[T2,Y]=ode23(@odefunction,[0 5],[0 5],options(i,j));
[T3,Z]=ode15s(@odefunction,[0 5],[0 5],options(i,j));
data{i,j}.T1 = T1;
data{i,j}.X = X;
data{i,j}.T2 = T2;
data{i,j}.Y = Y;
data{i,j}.T3 = T3;
data{i,j}.Z = Z;
end
Try the above and see what happens. As an aside, you may want to use indexing variables other than i and j which MATLAB uses to represent the imaginary number.
  1 commentaire
Geoff Hayes
Geoff Hayes le 8 Mar 2015
Martin's answer moved here
Thank you very much, I thought I had to use the cell command but I didn't know how to use it properly. Your code works perfectly, this is exactly what I wanted.
By the way, thanks for the advice, I'll rename the two variables used for the loops.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by