If Statement to meet 8 Conditions
5 Comments
@Karl Reuth: what do you intend this logic to do?:
(channel1(1:10) == (min1>=40) && (max1<=48))
The logical comparison operators have true/false output values, so this will test if the first ten values of channel against whether min1 is greater or equal to 40.... and then all of those true/false value against whether max1 is less than 48. There are two cases when this entirety gives a true output:
- channel1 is zero and min1<40 and max1<=48
- channel1 is one and min1>=40 and max1<=48
otherwise it will be false. For example:
>> (1 == (true) & (true)) ans = 1 >> (0 == (false) & (true)) ans = 1 >> (0 == (true) & (true)) ans = 0
https://www.mathworks.com/help/matlab/matlab_prog/operator-precedence.html
And of course the short circuiting operators && and || do not work with non-scalar arrays, so perhaps you should replace them with & and |.
In addition to Stephen's comment, whenever you start numbering variables you need to stop and change your approach. Have only one variable channel, only one variable channelmax (don't call it max!), one variable channelmin (don't call it min!), that you index. If all your channelxxx have the same length, then the simplest is to create a 2D array.
If you do that your conditional expression, whatever it should be, will be a lot easier to write. Unfortunately, as per Stephen's comment, at the moment your expressions are meaningless and we have no idea what you wanted to do.
Also, please clarify if minxxx and maxxx are scalar or vectors (and if they're vectors, why you're not extracting the first 10 elements only).
Answers (1)
tests = false(8,1); test(1) = all(channel1(1:10)>=min1 & channel1(1:10)<=max1); test(2) = all(channel2(1:10)>=min2 & channel2(1:10)<=max2); test(3) = all(channel3(1:10)>=min3 & channel3(1:10)<=max3); test(4) = all(channel4(1:10)>=min4 & channel4(1:10)<=max4); test(5) = all(channel5(1:10)>=min5 & channel5(1:10)<=max5); test(6) = all(channel6(1:10)>=min6 & channel6(1:10)<=max6); test(7) = all(channel7(1:10)>=min7 & channel7(1:10)<=max7); test(8) = all(channel8(1:10)>=min8 & channel8(1:10)<=max8); if all(tests) disp('The Signal is coming from the Baby Finger'); else disp('The Signal is NOT coming from the Baby Finger'); end
2 Comments
Really, what needs to be changed into arrays is the inputs. If channel was a 2d array and the minxx and maxxx were column vectors the whole mess would simplify to:
%channel a 2D array where row x corresponds to channelx %minchannel a column vector where row x corresponds to minx %maxchannel a column vector where row x corresponds to maxx
if all(channel(:, 1:10) >= minchannel & channel(:, 1:10) <= maxchannel)) disp('condition fulfilled'); else disp('condition not fulfilled');
end
That's it very simple if test with no risk of mistyping. You can also easily increase the number of channels without having to edit the test at all.
See Also
Categories
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!