Search All of the Math Forum:

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

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

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,850 Registered: 6/7/07
Re: using for loop to decide if is pass or fail
Posted: Mar 9, 2013 1:51 PM

On 3/8/2013 8:14 AM, dpb wrote:
> On 3/8/2013 5:56 AM, NatasaSI wrote:
...

>> Now i have the result that i need. How can i conclude the test like this:
>> if all elements are PASS than write 'Test: PASS', otherwise 'Test:FAIL'?
>>

...

> Again, the Matlab way...
>
> if all(iswithin(x,Lo,Hi))
> disp('Test: PASS')
> else
> disp('Test: FAIL')
> end
>
> Don't need any loops...
>

...

And, of course, you can eliminate the if...else...end clause easily as well.

disp(['TEST ' char(lbl(all(iswithin(x,lo,hi))+1))])

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

NB: One has a choice -- one can eliminate the cast to char() if one
makes the label array a character array instead of cell strings, but
then the subscripting has to use the 2nd dimension ':' to get the full
string, not just the first character. It seems awkward to me there
isn't an overloaded horzcat for cell strings that "just works"...

--

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