Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
sum of a vector if two conditions are met

Subject: sum of a vector if two conditions are met

From: Rogelio

Date: 10 Jul, 2012 22:11:13

Message: 1 of 7

Hi!
I have a question ... is it possible to do something like this: (without loops and if statements)
isOK = strcmp(C_data{1,1},'null') && strcmp(C_data{1,20},'1');
myvector = str2num(C_data{1,10})
sum(myvector(isOK));

it works if i only have isOK = strcmp(C_data{1,1},'null'), i.e. one condition. But i need to add myvector if both conditinos are met.

Subject: sum of a vector if two conditions are met

From: dpb

Date: 10 Jul, 2012 23:35:32

Message: 2 of 7

On 7/10/2012 5:11 PM, Rogelio wrote:
> Hi! I have a question ... is it possible to do something like this:
> (without loops and if statements)
> isOK = strcmp(C_data{1,1},'null') && strcmp(C_data{1,20},'1'); myvector
> = str2num(C_data{1,10})
> sum(myvector(isOK));
>
> it works if i only have isOK = strcmp(C_data{1,1},'null'), i.e. one
> condition. But i need to add myvector if both conditinos are met.

So what's wrong w/ an IF?

The use of isOK as is is as a logical subscript and will simply sum
whatever elements of myvector() for which it is true (of course, if the
length of isOK isn't within the bounds of myvector() it'll error.

--

Subject: sum of a vector if two conditions are met

From: Bruno Luong

Date: 11 Jul, 2012 07:11:21

Message: 3 of 7

dpb <none@non.net> wrote in message <jtie8e$59o$1@speranza.aioe.org>...

>
> The use of isOK as is is as a logical subscript and will simply sum
> whatever elements of myvector() for which it is true (of course, if the
> length of isOK isn't within the bounds of myvector() it'll error.

isOK is NOT a logical array, since it is created with the shortcut AND (&&).

OP fails to provide the detail about the input data (class, size), the code reading is ambiguous for us.

But I have seen that happens too often in CSSM.

Bruno

Subject: sum of a vector if two conditions are met

From: dpb

Date: 11 Jul, 2012 13:27:07

Message: 4 of 7

On 7/11/2012 2:11 AM, Bruno Luong wrote:
> dpb <none@non.net> wrote in message <jtie8e$59o$1@speranza.aioe.org>...
>
>>
>> The use of isOK as is is as a logical subscript and will simply sum
>> whatever elements of myvector() for which it is true (of course, if
>> the length of isOK isn't within the bounds of myvector() it'll error.
>
> isOK is NOT a logical array, since it is created with the shortcut AND
> (&&).
...

Yeah, I don't have '&&' and while I remembered it "short circuits" I had
forgotten about it also reducing to a single logical.

But, it _is_ still a logical array, just a logical array of size=(1,1) :)

--

Subject: sum of a vector if two conditions are met

From: dpb

Date: 11 Jul, 2012 13:34:03

Message: 5 of 7

On 7/10/2012 5:11 PM, Rogelio wrote:
> Hi! I have a question ... is it possible to do something like this:
> (without loops and if statements)
> isOK = strcmp(C_data{1,1},'null') && strcmp(C_data{1,20},'1'); myvector
> = str2num(C_data{1,10})
> sum(myvector(isOK));
>
> it works if i only have isOK = strcmp(C_data{1,1},'null'), i.e. one
> condition. But i need to add myvector if both conditinos are met.

Actually, on reflection I think it could assuming the caveats Bruno
mentions on the arrays being conformant in type/sizes if you use '&'
instead of '&&'

--

Subject: sum of a vector if two conditions are met

From: Bruno Luong

Date: 11 Jul, 2012 14:02:27

Message: 6 of 7

dpb <none@non.net> wrote in message <jtjuvl$er0$1@speranza.aioe.org>...
> On 7/11/2012 2:11 AM, Bruno Luong wrote:

>
> But, it _is_ still a logical array, just a logical array of size=(1,1) :)
>

Fair enough ;-)

Bruno

Subject: sum of a vector if two conditions are met

From: Rogelio

Date: 11 Jul, 2012 19:25:07

Message: 7 of 7

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jtk11j$rd6$1@newscl01ah.mathworks.com>...
> dpb <none@non.net> wrote in message <jtjuvl$er0$1@speranza.aioe.org>...
> > On 7/11/2012 2:11 AM, Bruno Luong wrote:
>
> >
> > But, it _is_ still a logical array, just a logical array of size=(1,1) :)
> >
>
> Fair enough ;-)
>
> Bruno

C_data{,} is a matrix of size 144013 x 30. It is the output of textscan function in matlab. All of the columns are strings.

I can obtain what I need in this way

for i =1:N
a=strcmp(C_data{1,1}{i,1},'9');
b=strcmp(C_data{1,26}{i,1},'1');
if (a==1)&&(b==1)
final_vector(i)=C_data{1,10}{i,1};
end
end


The "problem" is that i use 100 different final_vectors (and sum(final_vector) to plot more than 100 qq plots. So to write the above loop 100 time will be tedious. Tough I could try to do everything at once, just in one loop. The final_vector, then the qq plot etc. But I believe that it will be slow....

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us