How to filter a datasheet?

2 次查看(过去 30 天)
I have imported a datasheet from excel in the form of table (130 * 23).
Each column represents a design parameter of robotic actuator (i.e. Continuous Torque, Speed, weight, Reduction ratio, etc.)
I want to filter my table based on some constraints like, I want to extract only those rows where torque value is between 1 to 5 and weight value is between 0 to 1 and so on.
For that I wrote this:
FinalTable = compileddatasheet((compileddatasheet.ContinuousTorqueNm > 1) & (compileddatasheet.ContinuousTorqueNm < 5) & (compileddatasheet.Weightkg > 0) & (compileddatasheet.Weightkg < 1), :);
This code works fine.
But,
Now I want to add one more constraint, see the code below
FinalTable = compileddatasheet(a*(compileddatasheet.ContinuousTorqueNm > 1) & b*(compileddatasheet.ContinuousTorqueNm < 5) & c*(compileddatasheet.Weightkg > 0) & d*(compileddatasheet.Weightkg < 1), :);
As you can see in the above code i added 4 binary variables (a,b,c,d) which can be either 0 or 1.
I added these variables so I can decide whether I want to use the particular constraint or not based on its value (i.e. if a = 0 and other variables are still 1, results will be shown based on only three constraints).
So my approach here is wrong because its not showing the true results.
So it would be of great help if anyone could help me out here with a proper approach.
Thank you.

采纳的回答

Walter Roberson
Walter Roberson 2019-9-10
Each time you set one of the binary variables to 0, then 0*(the logical value of the expression next to it) is going to be 0, which is false, and expression & false is going to be false. Therefore each time you set one of those binary variables to 0, you make the entire expression false.
Change the general form
binary_variable * logical_expression
into
(~binary_variable | logical_expression)
  3 个评论
Walter Roberson
Walter Roberson 2019-9-10
c = 0
true & c * (5==4) & true
ans = false
so setting c to 0 did not turn off the 5==4 test -- if it had somehow turned it off then the true & true on both sides should lead to an overall true result.
However,
>> c = 0; true & (~c | (5==4)) & true
ans =
logical
1
Here the 5==4 test has been turned off, leaving the true & true to come out true.
>> c = 1; true & (~c | (5==4)) & true
ans =
logical
0
Here the 5==4 test has been turned on, leading to true & false & true which is overall false.
Jasraj Soni
Jasraj Soni 2019-9-10
It works!!
Thank you so much Walter for your time.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Optimization 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by