variable number of if condition

9 visualizaciones (últimos 30 días)
Francesco
Francesco el 25 de Mayo de 2022
Editada: Francesco el 26 de Mayo de 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...
  1 comentario
Stephen23
Stephen23 el 25 de Mayo de 2022
Editada: Stephen23 el 25 de Mayo de 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...

Iniciar sesión para comentar.

Respuesta aceptada

Steven Lord
Steven Lord el 25 de Mayo de 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.

Más respuestas (2)

Bjorn Gustavsson
Bjorn Gustavsson el 25 de Mayo de 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 comentarios
Francesco
Francesco el 25 de Mayo de 2022
Thank, I will try.
First of all, It doesn't smell, it stinks a lot :D you could say it, but I understand you are a kind person.
I am not a programmer (that's why I asked this trivial question), I know it can be written in a much better way and I will do, but I need to start from some basic working thing and then try to make it less dodgy and efficient. At the end the script is very simple (infact I was able to write it and it does what I need)
BTW, a little more explanation: Ex is the matrix with x data. It has several colums and each column is a single set of data. Then I have another file Ey, with the conresponding y data. The first for (with the "i" counter) is a loop I wrote to "select" which data range I want to analyse. I use min and max limit then the column. (if I want to use the first 10 data, then min=1 and max=10)
I think I should have a better organized dataset, I don't like so much the fact that the x and y data are in two files... I think that if I have one file for each x and y data set I could just load the files I need not everything and then manipulate the portion I am interested in.
The second loop (with the "a" counter) is on the specific set of data, so basically it takes all the data from the column "i".
Saying that, I think your suggestion is quite clear, I will have a matrix with the limitis (cond_lims) and I will create all the if-conditions in the your new for loop, if I got it right.
I just don't understand very well this QWE{i,a,i_conds}, could you explain me?
My "do something" is a few-lines piece of code I use to temporary store the data from Ex and Ey in that interval and make some manipulation that I will need later when I calculate the integrals of the data in the intervals.
THanks a lot for your help
Bjorn Gustavsson
Bjorn Gustavsson el 25 de Mayo de 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

Iniciar sesión para comentar.


Francesco
Francesco el 26 de Mayo de 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.
  1 comentario
Francesco
Francesco el 26 de Mayo de 2022
Editada: Francesco el 26 de Mayo de 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;
.....

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by