If Statement to meet 8 Conditions

2 ビュー (過去 30 日間)
Karl Rueth
Karl Rueth 2018 年 4 月 20 日
コメント済み: Karl Rueth 2018 年 4 月 22 日
I have 8 Conditions which must be met in order to display the text ... What I am doing wrong ?
Thanks for any help.
  5 件のコメント
Stephen23
Stephen23 2018 年 4 月 20 日
編集済み: Stephen23 2018 年 4 月 20 日

@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 |.

Guillaume
Guillaume 2018 年 4 月 20 日

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).

サインインしてコメントする。

回答 (1 件)

Are Mjaavatten
Are Mjaavatten 2018 年 4 月 21 日
編集済み: Are Mjaavatten 2018 年 4 月 21 日
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 件のコメント
Guillaume
Guillaume 2018 年 4 月 22 日

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.

Karl Rueth
Karl Rueth 2018 年 4 月 22 日
Thanks for your help, in the process I worked with this method;
if (((channel1(10) >= 38) && (channel1(1) <= 50))&& ((channel2(10) >= 38) && (channel2(1) <= 52)) && ((channel3(10) >= 8) && (channel3(1) <= 16)) && ((channel4(10) >= 8) && (channel4(1) <= 18)) && ((channel5(10) >= 16) && (channel5(1) <= 28)) && ((channel6(10) >= 27) && (channel6(1) <= 42)) && ((channel7(10) >= 19) && (channel7(1) <= 33)) && ((channel8(10) >= 22) && (channel8(1) <= 39))) disp('The Signal is coming from the Baby Finger');
Worked fine

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeResults, Reporting, and Test File Management についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by