Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: for loop to find true/false and build sum and so distinguish
Replies: 10   Last Post: Apr 13, 2012 1:36 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
dpb

Posts: 8,250
Registered: 6/7/07
Re: for loop to find true/false and build sum and so distinguish
Posted: Apr 11, 2012 1:12 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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)

--



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.