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: for loop to find true/false and build sum and so distinguish
Replies: 10   Last Post: Apr 13, 2012 1:36 PM

 Messages: [ Previous | Next ]
 dpb Posts: 9,850 Registered: 6/7/07
Re: for loop to find true/false and build sum and so distinguish
Posted: Apr 11, 2012 1:12 PM

On 4/11/2012 10:19 AM, Markus wrote:
>> % From what I understand,
>> % the above code becomes something like this:
>>
>> myvalue = ??; % length 16 vector, insertvalues here
>> Number = ??; % scalar, insert value here
>> truefalse = zeros(1,16); % initialize to all zeros (for false case)
>> for i =1:16
>> if myvalue(i) > Number % true case
>> truefalse = 1;
>> end
>>
>> n=sum(truefalse); % =max 16, min 0
>> if n==0
>> option 1 % insert your code here
>> elseif n==6
>> option 2 % insert your code here
>> elseif n==16
>> option 3 % insert your code here
>> end
>>
>> % The above may not be exactly what you want,
>> % but its my best interpretation and
>> % it should get you started.

>
>
>
> Thanks very much to someone :o) I am checking soon if it works, will let
> you know! Cheers and thanks! Markus

If the array is full and the limit is indeed fixed, then

>> for i =1:16
>> if myvalue(i) > Number % true case
>> truefalse = 1;
>> end
>>
>> n=sum(truefalse); % =max 16, min 0

is simply

flg = myvalue>Number;
n=sum(flg);

What to do next depends greatly on what the intent is beyond this; if
it's a similar function w/ simply different input value then writing
that to work on the value as a single function would be apropos;
otherwise perhaps a CASE structure of something else entirely could be
the preferred "trick".

In general though, for...end is not "the_Matlab_way" (tm)

--

Date Subject Author
4/11/12 Markus
4/11/12 Curious
4/11/12 Markus
4/11/12 dpb
4/11/12 Curious
4/11/12 dpb
4/12/12 Markus
4/12/12 Steven Lord
4/13/12 Curious
4/13/12 Markus
4/13/12 dpb