How to optimize IF statement with Multiple Conditions.
Show older comments
Hello,
I have five Tables (T1 to T5) and Conditons CR ranging from ( 0-4) . When I process a files each file may have one condition in it or multiple conditons. For example ( it could have Condition 3 or in the next file it could be 0,2 & 4). Which ever condtiion is satisfied that particular table (if one condition) and (Multiple table Verticat into one final table).
I tried using multiple methods like
1) If any or all statments didn't work.
2) if I have to use ismember then I could have make multiple array of condition hard coded into the script. Example: Condition:if ismember([1 2], CR]'
Final_Table = verticat(T2;T3).
Could someone suggest a robust and quicker way.
If possible provide with a Script sketon or example so It could be understood better and also your time and effort is well respected.
Thank you
8 Comments
As per usual, you'll have a let better luck in getting useful responses if you will attach representative data files so that people here can actually see what you have that you're trying to describe. Then illustrate for those files what the expected result would be.
One note is that if by "I have five Tables (T1 to T5) " you mean you have created five MATLAB variables T1 through T5 and are trying to operate on them programmatically making logic decisions based on the variable name or even just processing them in a similar fashion, creating sequentially-numbered variables in this fashion is almost never the right way to go...instead, use an array of tables or merge what is/are the unique variable/(s) into the one overall table as additional variables with the distinguishing value(s). Then you can operate by those characteristic values without having to duplicate code with various combinations of hardcoded variables as you are illustrating above.
But, rather than us trying to guess; provide the example data files and it's likely there will be pretty painless ways to do what you're atttempting.
ADDENDUM:
But, if the issue were to boil down to a case of an if construct with multiple conditions, the most likely way to simplify it would be with a switch, case, otherwise construct rather than an if, elseif, else. But, given the description, I don't think your problems revolve around that choice but in how you're handling the data files initially.
Sai Gudlur
on 26 Sep 2024
Sai Gudlur
on 26 Sep 2024
Note that FOR actually iterates over the columns of the provided array:
So your code with its (convoluted indirectly defined) column vectors will iterate exactly once for each FOR loop:
for Active_Slot_Ids= [0,1,2,3,4]'
disp('before')
disp(Active_Slot_Ids)
disp('after')
end
That is not five separate iterations, that is exactly one iteration. Which means that none of your loops do anything useful, they are entirely superfluous.
If you want multiple iterations (with a scalar each time) then supply a row vector:
for Active_Slot_Ids = 0:4
disp('before')
disp(Active_Slot_Ids)
disp('after')
end
Yes, this "feature" makes no sense. Yes, it causes more bugs than it has ever been useful. Yes, TMW should get rid of it.
Sai Gudlur
on 26 Sep 2024
"Thanks for your time but I cannot have FOR loop in Range of 0:4 as sometimes Active_Slots_Ids may not have numbers in sequence. For Example in the Script I have the 3rd FOR Loop as below. And each Active Slot_id signifies certain table and only the the digit present need to be put together in Complete_Table."
Nothing in my comment is about the values your loop uses. Use whatever values you want.
My comment explains why the orientation of your vectors is very unlikely to do what you want. And in the unlikely case that you do want that behavior, then your FOR loops are entirely superfluous. Which seems unlikely to me.
In any case, the values you are using has nothing to do with my comment:
for Active_Slot_Ids = [0,2,3]' % this will not work. The values are not the reason.
Sai Gudlur
on 26 Sep 2024
dpb
on 26 Sep 2024
I would venture there are ways to reduce the tedium, but you forgot to attach sample data files for anybody to be able to look at to see how to make more efficient...
Answers (0)
Categories
Find more on MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!