# variable number of if condition

4 views (last 30 days)
Francesco on 25 May 2022
Edited: Francesco on 26 May 2022
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...
Stephen23 on 25 May 2022
"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...

Steven Lord on 25 May 2022
Use discretize to determine into which bin each of your data sets belongs. Then you could use groupsummary, grouptransform, a simple loop over the bins, or perhaps even that same discretize call to "do something" to the data in each group, depending on what "do something" is.

Bjorn Gustavsson on 25 May 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 CommentsShowHide 1 older comment
Bjorn Gustavsson on 25 May 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

Francesco on 26 May 2022
Thanks to all of you guys, for advices and criticism, I appreciate them all.
I will check the documentation for the discretize function and I will try to improve the frangrance of the whole thing (which was in my plans anyway).
I will be back here if I have questions.
I have enough input to move on, I would say
Cheers
F.
Francesco on 26 May 2022
... and here I have the first question.
I was reading the discretize function, I think I understand how to use it but, at the same time I was keep on using my original script for the data analysis and I faced one problem.
If two intervals in the if conditions overlap eachother I have an error!
I did something like this and obviously the second and next loops are skipped.
Now, it is clear that my strategy is going beyond the stink (Bjorn, sorry for saying it, but you were wrong! :D) and the question is: would discretize help me? I did not understand this from the documentation. or Shall I use something different?
I tested just this case, but I think the issue is clear and I can say that if two intervals overlaps partially, then the operation on the second one are performed on a smaller range (the part of data that do not overlap).
Thanks
F.
for a=1:length(Ex)
if Ex(a,i)>=1 %Takes all data bigger than 1
do something
b=b+1;
elseif Ex(a,i)>=4.5 && Ex(a,i)<=5.5 %should takes all data between 4.5 and 5.5 but does nothing
do something
d=d+1;
.....