Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Topic: using for loop to decide if is pass or fail
Replies: 5   Last Post: Mar 19, 2013 1:49 PM

 Messages: [ Previous | Next ]
 dpb Posts: 9,331 Registered: 6/7/07
Re: using for loop to decide if is pass or fail
Posted: Mar 19, 2013 1:49 PM

On 3/19/2013 10:28 AM, NatasaSI wrote:
...

>> To avoid the overhead of calling iswithin() twice, save the resulting
>> logical vector the first time and use it in place of the explicit call...
>>
>> lbl = {'FAIL';'PASS'};
>> flg=iswithin(x,lo,hi);
>> disp([num2str(x) repmat(' ', [size(x)]) char(lbl(flg+1))])
>> disp(['TEST ' char(lbl(all(flg)+1))])
>>

...

> I try to do insert this:
>
> if all(iswithin(x,limitA,limitB))
> disp('Test: PASS')
> else
> disp('Test: FAIL')
> end
>
> and this:
>
> lbl = {'FAIL';'PASS'};
> flg=iswithin(x,limitA,limitB);
> disp([num2str(x) repmat(' ', [size(x)]) char(lbl(flg+1))])
> disp(['TEST ' char(lbl(all(flg)+1))])
>
> so my final code is like this:
> limitA= 0.996
> limitB= 0.0013
> lbl={'FAIL';'PASS'};
>
> for k=1:length(frekvenca)
> x=matrika2(k,1);
> disp([num2str(x) repmat(' ', [size(x)]) ...
> char(lbl(iswithin(x,limitA,limitB)+1))])
> end
>
>
> % if all(iswithin(x,limitA,limitB))
> % disp('Test: PASS')
> % else
> % disp('Test: FAIL')
> % end
>
> flg=iswithin(x,limitA,limitB);
> disp([num2str(x) repmat(' ', [size(x)]) char(lbl(flg+1))])
> disp(['TEST ' char(lbl(all(flg)+1))])
>
> The output is like:
> 0.25832 PASS
> 0.27942 PASS
> 0.99918 FAIL

...
> 0.11005 PASS
> 0.29027 PASS
> TEST PASS
> Even if there is one FAIL the result is TEST PASS. Where is the problem?
> Thanks for help

You seem not to be paying any attention here, unfortunately... :(

You don't need the loop (and it's getting in the way) -- use the array
directly as shown before. If the upper limit length(frekvenca) isn't
the same as size(matrika2,2) (which would be the more logical value to
use for the loop bound it would seem given the code but sometimes one
doesn't want to process the entire array, granted, so if that's the case
then ignore this sidebar) then use matrika2(1:frekvenca,1) in the
vectorized solution.

The reason you're getting 'PASS' for the last case is that you're
testing the value of x which is the single value last processed by the
loop _NOT_ the full complement and since the last individual case is a
success then it's going to be when it's tested again. If the order were
such that the last element were to not pass then you would get 'FAIL'
for every set of data instead no matter what set of data it were and the
fact the answer appears correct would simply be happenstance. For that
reason you were lucky the case data was as it is; you caught the error
in having the wrong result.

In my previous examples I used "x" as a shorthand for your subarray on
the assumption you could take that and apply it to your problem. I'll
leave you to do that.

--

Date Subject Author
3/8/13 dpb
3/9/13 dpb
3/19/13 dpb