variable number of if condition
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2022-5-25
编辑:Stephen23
2022-5-25
"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
2022-5-25
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.
0 个评论
更多回答(2 个)
Bjorn Gustavsson
2022-5-25
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 个评论
Bjorn Gustavsson
2022-5-25
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!