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:
simple if-then-else question

Subject: simple if-then-else question

From: Kirk

Date: 20 Jan, 2009 18:51:02

Message: 1 of 6

I have a fairly long (13956 X 1) array as a component of a struct that contains a few negative values. I need to replace the negative values with zero. I thought that this would be a good case for an if statement. However, I must be using the wrong syntax.

I tried:

if struct.x < 0
struct.x = 0
end

would do it. But I was wrong. Can anyone tell what I missed?

Thanks.

Subject: simple if-then-else question

From: Walter Roberson

Date: 20 Jan, 2009 18:59:33

Message: 2 of 6

Kirk wrote:
> I have a fairly long (13956 X 1) array as a component of a struct that contains a
> few negative values. I need to replace the negative values with zero. I thought that
> this would be a good case for an if statement. However, I must be using the wrong syntax.
 
> I tried:
 
> if struct.x < 0
> struct.x = 0
> end
 
> would do it. But I was wrong. Can anyone tell what I missed?

struct.x(struct.x < 0) = 0;

Or,

struct.x = max(struct.x, zeros(size(struct.x)));

--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: simple if-then-else question

From: Matt Fig

Date: 20 Jan, 2009 19:00:19

Message: 3 of 6

If the array is part of a struct, you need to address that field.

struct.array = round(rand(3)*5)-round(rand(3)*5)
struct.array(struct.array<0)=0




"k`jo]];`\zzzjz\\@dodbk5cz)\p\hhzfojjp"ti^q\jcz`ic\jgTH`g^z

Subject: simple if-then-else question

From: Matt

Date: 20 Jan, 2009 19:00:19

Message: 4 of 6

"Kirk" <kwythers.nospam@umn.edu> wrote in message <gl56em$51p$1@fred.mathworks.com>...

> if struct.x < 0

This if-block will get executed only if ALL elements of struct.x are <0.

What you want is

struct.x(struct.x<0)=0;

Subject: simple if-then-else question

From: Kirk

Date: 20 Jan, 2009 20:25:05

Message: 5 of 6


> > if struct.x < 0
>
> This if-block will get executed only if ALL elements of struct.x are <0.
>
> What you want is
>
> struct.x(struct.x<0)=0;
>

Thanks everyone.

Would the interpretation of the above code be something along the lines of:

"set all elements that are < 0, in struct.x equal to 0"?

Subject: simple if-then-else question

From: Sabine

Date: 20 Jan, 2009 20:38:02

Message: 6 of 6

"Kirk" <kwythers.nospam@umn.edu> wrote in message <gl5bv1$rv2$1@fred.mathworks.com>...
>
> > > if struct.x < 0
> >
> > This if-block will get executed only if ALL elements of struct.x are <0.
> >
> > What you want is
> >
> > struct.x(struct.x<0)=0;
> >
>
> Thanks everyone.
>
> Would the interpretation of the above code be something along the lines of:
>
> "set all elements that are < 0, in struct.x equal to 0"?
>
>
Yes, that's true. It reads:
All elements of struct.x which fullfill the criterion 'struct.x < 0' are set to zero.

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