variable number of if condition
Afficher commentaires plus anciens
Hello,
I have a set of data (two columns, first is x and second is y) and in a for loop I am analyzing them doing different things according to a certain range of x values. I do something like this:
for i=min:max%:columns
b=1;
d=1;
f=1;
for a=1:length(Ex)
if Ex(a,i)>=1.8 && Ex(a,i)<=2.2
do something
b=b+1;
elseif Ex(a,i)>=4.5 && Ex(a,i)<=5.5
do something
d=d+1;
elseif Ex(a,i)>=7.2 && Ex(a,i)<=8.8
do something
f=f+1;
end
end
calculate the integrals of the previosu "do something results"
end
So basically I have 3 if.
but what if I want to change the number of intervals everytime I do this analysis?
Today I need 3 intervals, tomorrow maybe 10, after tomorrow maybe 1...
What I would like to do is setting the number of "if" condition as a variable, but I don't how to do that.
I mean, for a fixed number of intervarls is easy, I could manually write milions of loops and ask for their limits, but how to make it general?
I don't know if I explain my question properly...
1 commentaire
"but how to make it general?"
Work with arrays rather than loops and IFs, e.g. consider using DISCRETIZE or perhaps HISTCOUNTS or similar.
Take a look at the documentation for ACCUMARRAY and GROUPSUMMARY, also check out the linked functions and articles at the bottom of each documentation page. Your explanation is too general to be more specific...
Réponse acceptée
Plus de réponses (2)
Bjorn Gustavsson
le 25 Mai 2022
Maybe something like this:
for i = min:max %:columns
b = 1;
d = 1;
f = 1;
for a = 1:length(Ex)
for i_conds = 1:size(cond_lims,1)
if Ex(a,i) >= cond_lims(i_conds,1) && Ex(a,i) <= cond_lims(i_conds,2)
QWE{i,a,i_conds} = "do something";
cond_counter(i,i_conds) = cond_counter(i,i_conds) + 1;
end
end
end
calculate the integrals of the previosu "do something results"
end
You'll have to figure out how to best collect the results after the inner loop. That should be doable, but this does smell of a dodgy program-design.
HTH
2 commentaires
Francesco
le 25 Mai 2022
Bjorn Gustavsson
le 25 Mai 2022
My QWE{i,a,i_conds} is just a cell-array (where you can chuck just about any thing in the different cells, and since I didn't know what your "do something" would result in and if that result could be put into an ordinary regular array I opted for this all-purpose multi-content container). Then after that looping you would have to extract and combine the relevant cells into the format you need for the calculation of the integral.
Have a good look at the discretize function that Steven and Stephen suggest, and also (since you understand the "fragrance of your situation") perhaps you can make a cleaner algorithm if you sit down with pen and paper and sketch things out for 30 minutes - an hour. Vast improvements can sometimes be made once a QD-solution has been made.
HTH
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!