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:
how to improve this code

Subject: how to improve this code

From: Patrick Diviacco

Date: 1 Oct, 2009 08:35:02

Message: 1 of 4

hi,

is there any better way to write this in MATLAB ?

if (binNumber2 == 0)
   binNumber2 = 1;
end

thanks

Subject: how to improve this code

From: Jos

Date: 1 Oct, 2009 08:46:01

Message: 2 of 4

"Patrick Diviacco" <patrick.diviacco@gmail.com> wrote in message <ha1pjm$glg$1@fred.mathworks.com>...
> hi,
>
> is there any better way to write this in MATLAB ?
>
> if (binNumber2 == 0)
> binNumber2 = 1;
> end
>
> thanks

There are several ways of doing things like this. It also depends what you want to do if binNumber2 is not zero, can have other values than 0 or 1, and whether it is an array or not.

A = true, A = ~A,
B = 1, B = 1-B
C = [0 11 12 13 14], C(C==0) = 1
etc.

hth
Jos

Subject: how to improve this code

From: TideMan

Date: 1 Oct, 2009 10:28:20

Message: 3 of 4

On Oct 1, 9:46 pm, "Jos " <#10...@fileexchange.com> wrote:
> "Patrick Diviacco" <patrick.divia...@gmail.com> wrote in message <ha1pjm$gl...@fred.mathworks.com>...
> > hi,
>
> > is there any better way to write this in MATLAB ?
>
> > if (binNumber2 == 0)
> >    binNumber2 = 1;
> > end
>
> > thanks
>
> There are several ways of doing things like this. It also depends what you want to do if binNumber2 is not zero, can have other values than 0 or 1, and whether it is an array or not.
>
> A = true, A = ~A,
> B = 1, B = 1-B
> C = [0 11 12 13 14], C(C==0) = 1
> etc.
>
> hth
> Jos

And of course these methods apply if and only if the numbers are
integers.
If the numbers are floating point, then these methods don't work at
all.

Subject: how to improve this code

From: Rune Allnor

Date: 1 Oct, 2009 10:45:04

Message: 4 of 4

On 1 Okt, 10:35, "Patrick Diviacco" <patrick.divia...@gmail.com>
wrote:
> hi,
>
> is there any better way to write this in MATLAB ?
>
> if (binNumber2 == 0)
>    binNumber2 = 1;
> end

No.

You can mask the statement in a depressingly large number
of guises, but at the end of the day, your statement above
is what actually happens.

Rune

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