dpb
Posts:
9,219
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)


