Storing lots of data with nested for loops

Hello! I need some help figuring out how to store data for nested for loops. I know this is a simple problem and please forgive my ignorance but I need to store a million pieces of data (each vector is has a length of 100, cube that and you get a million pieces of data) and filter out what I want to keep in separate arrays using a conditional statement.
I'm not so concerned about the later right now, I just need to figure out a way to store everything and not have it reset everytime i or j goes on to the next iteration. Can some please help? I know my algorithm is logically correct in what I want it to do.
clc
clear
deltaVmax=50;
deltaVmin=8;
ISP=linspace(250,750);
Thrust=linspace(4500,45000);
dt=linspace(5:15:100)
g_sun=274;
n1=length(ISP);
n2=length(Thrust);
n3=length(dt);
n4=n1^3;
Mi=250;
for i=1:n1
for j=1:n2
for k=1:n3
deltaV(k)=g_sun*ISP(i)*log((Mi)/(Mi-(dt(k)*Thrust(j))/(g_sun*ISP(i))))
if deltaV(k)>=deltaVmin && deltaV(k)<=deltaVmax
dV_want=deltaV(k);
dt_want=dt(k);
Isp_want=Isp(i);
T_want=Thrust(j);
end
end
end
end

1 commentaire

Stephen23
Stephen23 le 28 Nov 2017
Modifié(e) : Stephen23 le 28 Nov 2017
"...I want to keep in separate arrays..."
Doing that would give you slow, complex, buggy code. Instead of storing your data in lots of separate varaibles and magically trying to access them in a loop, why not simply store your data in one array using simple and efficient indexing.
"...I know this is a simple problem..."
and the solution is not to turn this simple problem into complex ugly code which magically accesses variable names. Just use indexing (because that is the simple solution to your simple problem).

Connectez-vous pour commenter.

 Réponse acceptée

Geoff Hayes
Geoff Hayes le 28 Nov 2017
Michael - why not store your deltaV in a 100x100x100 array? You could then do the same for the other data that you want to keep. For example,
deltaV = zeros(n1,n2,n3);
for i=1:n1
for j=1:n2
for k=1:n3
deltaV(i,j,k) = g_sun*ISP(i)*log((Mi)/(Mi-(dt(k)*Thrust(j))/(g_sun*ISP(i))));
% etc.
end
end
end
All data from every iteration is stored in deltaV.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by