Got Questions? Get Answers.
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:
even and odd numbers

Subject: even and odd numbers

From: Rubi

Date: 25 Jan, 2009 21:15:07

Message: 1 of 13

Hi and thanks for any idea or code !

Im new at matlab
I have an .m file UY=[0.513671875
0.620666504
115.847168
256.2485046
230.8596191
....]

And i have to separate in a new file the even numbers and odd numbers is that posible having decimals? i searched and i found this code

if mod(x,2) == 0
%number is even
else
%number is odd
end

Thank you :)

Subject: even and odd numbers

From: per isakson

Date: 25 Jan, 2009 21:26:03

Message: 2 of 13

"Rubi " <phreakadelik@hotmail.com> wrote in message <glikor$epo$1@fred.mathworks.com>...
> Hi and thanks for any idea or code !
>
> Im new at matlab
> I have an .m file UY=[0.513671875
> 0.620666504
> 115.847168
> 256.2485046
> 230.8596191
> ....]
>
> And i have to separate in a new file the even numbers and odd numbers is that posible having decimals? i searched and i found this code
>
> if mod(x,2) == 0
> %number is even
> else
> %number is odd
> end
>
> Thank you :)

The number, 115.847168, is that even or odd?

/per

Subject: even and odd numbers

From: Image Analyst

Date: 25 Jan, 2009 21:37:04

Message: 3 of 13

Not that I'm aware of. I've only ever heard of even and odd being applied to integers (whole numbers). I don't think it applies for non-integer floating point numbers having fractional parts. Why do you ask? Can you give some background?

Subject: even and odd numbers

From: Roger Stafford

Date: 25 Jan, 2009 21:57:04

Message: 4 of 13

"Rubi " <phreakadelik@hotmail.com> wrote in message <glikor$epo$1@fred.mathworks.com>...
> Hi and thanks for any idea or code !
>
> Im new at matlab
> I have an .m file UY=[0.513671875
> 0.620666504
> 115.847168
> 256.2485046
> 230.8596191
> ....]
>
> And i have to separate in a new file the even numbers and odd numbers is that posible having decimals? i searched and i found this code
>
> if mod(x,2) == 0
> %number is even
> else
> %number is odd
> end
>
> Thank you :)

 The quality of being even or odd has meaning only for integers. However, you can turn your numbers into integers using one of matlab's 'round', 'floor', or 'ceil' functions. The results of course will be dependent on which of these three you choose to use.

Roger Stafford
 

Subject: even and odd numbers

From: Rubi

Date: 27 Jan, 2009 18:24:01

Message: 5 of 13

Thanks a lot and well ill make them integers then :)

Subject: even and odd numbers

From: us

Date: 27 Jan, 2009 18:38:02

Message: 6 of 13

"Rubi "
> Thanks a lot and well ill make them integers then...

good - and in addition:
- don't use MOD or REM
- use BITAND instead

us

Subject: even and odd numbers

From: someone

Date: 27 Jan, 2009 19:00:21

Message: 7 of 13

"us " <us@neurol.unizh.ch> wrote in message <glnkaa$qnp$1@fred.mathworks.com>...
> "Rubi "
> > Thanks a lot and well ill make them integers then...
>
> good - and in addition:
> - don't use MOD or REM
> - use BITAND instead
>
> us

I'm sure there is a good reason, us, but why use bitand?

Subject: even and odd numbers

From: Matt Fig

Date: 27 Jan, 2009 19:06:03

Message: 8 of 13

http://www.mathworks.com/matlabcentral/newsreader/view_thread/174166#449381

Subject: even and odd numbers

From: us

Date: 28 Jan, 2009 08:58:01

Message: 9 of 13

"someone"
> > - don't use MOD or REM
> > - use BITAND instead
> I'm sure there is a good reason, us, but why use bitand...

matt (fig) already pointed to a thread discussing this issue at length...
in a nutshell

% BITMAX is the largest number, which gives a correct result...
     help bitmax

% the advantage of the BIT-family
% - they complain if something goes wrong, in particular: hidden/missed FP errors

% these will error out
     bitand(pi,1)
%{
     ??? Error using ==> bitand
     Inputs must be non-negative integers.
%}
     bitand(bitmax+1,1);
%{
     ??? Error using ==> bitand
     Exceeded value of bitmax.
%}

% whereas these don't care to warn you
     format hex;
     b=bitmax+(1:5).' % doubles tainted with FP errors
%{
     4340000000000000
     4340000000000000
     4340000000000001
     4340000000000002
     4340000000000002
%}
     format short g;
     mod(b,2)
% ans = 0 0 0 0 0 % !?
     rem(b,2)
% ans = 0 0 0 0 0 % !?

% therefore, this is preferable in my view
     v=-3:3;
     r=bitand(abs(v),1);
     disp([v;r]);
%{
    -3 -2 -1 0 1 2 3 % the data
     1 0 1 0 1 0 1 % is even
%}

just a thought...
us

Subject: even and odd numbers

From: Roger Stafford

Date: 29 Jan, 2009 03:53:01

Message: 10 of 13

"us " <us@neurol.unizh.ch> wrote in message <glp6mp$kq1$1@fred.mathworks.com>...
> .......
> % BITMAX is the largest number, which gives a correct result...
> help bitmax
> .......

  I agree with you, Urs. 'bitmax' would be the largest number that would give the correct answer as to a double floating point integer's least bit.

  However, it is rather amusing to read Mathworks' documentation for 'bitmax'. They say, "bitmax returns the maximum unsigned double-precision floating-point integer for your computer. It is the value when all bits are set, namely the value 2^53-1." This is wildly wrong! In fact every floating point number beyond that is of necessity an integer and what is more, an even integer. They meant to say something very different - something like: beyond this all numbers must have a zero in their units binary digit position and therefore can represent neither numbers with a fractional part nor odd integers.

Roger Stafford

Subject: even and odd numbers

From: Rubi

Date: 5 Feb, 2009 17:22:02

Message: 11 of 13

"Rubi " <phreakadelik@hotmail.com> wrote in message <glikor$epo$1@fred.mathworks.com>...
> Hi and thanks for any idea or code !
>
> Im new at matlab
> I have an .m file UY=[0.513671875
> 0.620666504
> 115.847168
> 256.2485046
> 230.8596191
> ....]
>
> And i have to separate in a new file the even numbers and odd numbers is that posible having decimals? i searched and i found this code
>
> if mod(x,2) == 0
> %number is even
> else
> %number is odd
> end
>
> Thank you :)

Very helpful,,!!!!!! and a lot of attention i appreciate every message , thanks :) !!!

Subject: even and odd numbers

From: Hamad Ali

Date: 26 Oct, 2012 15:22:08

Message: 12 of 13

"Rubi " <phreakadelik@hotmail.com> wrote in message <glikor$epo$1@fred.mathworks.com>...
> Hi and thanks for any idea or code !
>
> Im new at matlab
> I have an .m file UY=[0.513671875
> 0.620666504
> 115.847168
> 256.2485046
> 230.8596191
> ....]
>
> And i have to separate in a new file the even numbers and odd numbers is that posible having decimals? i searched and i found this code
>
> if mod(x,2) == 0
> %number is even
> else
> %number is odd
> end
>
> Thank you :)

I am so sorry cause the thread is out of date but I put my post here as it might be useful for some body else

I think for loop can give you a proper answer to your question and therefore try this small code

% Let's consider x as a raw vector consisting 10 elements;

x = [1 2 3 4 5 6 7 8 9 10];

for k = 1:length(x)
    y = x(mod(x,2)~=0);
    z = x(mod(x,2)==0);
end

disp('Even numbers in x are:' )
disp(z)
disp('Odd numbers in x are:' )
disp(y)

good luck

Subject: even and odd numbers

From: dpb

Date: 26 Oct, 2012 17:14:37

Message: 13 of 13

On 10/26/2012 10:22 AM, Hamad Ali wrote:
> "Rubi " <phreakadelik@hotmail.com> wrote in message
> <glikor$epo$1@fred.mathworks.com>...
>> Hi and thanks for any idea or code !
>>
>> Im new at matlab I have an .m file UY=[0.513671875
>> 0.620666504
>> 115.847168
>> 256.2485046
>> 230.8596191
>> ....]
>>
>> And i have to separate in a new file the even numbers and odd numbers
>> is that posible having decimals? i searched and i found this code
>> if mod(x,2) == 0
>> %number is even
>> else
>> %number is odd
>> end
...

> I am so sorry cause the thread is out of date but I put my post here as
> it might be useful for some body else
>
> I think for loop can give you a proper answer to your question and
> therefore try this small code
>
...

> for k = 1:length(x)
> y = x(mod(x,2)~=0);
> z = x(mod(x,2)==0);
> end
>
> disp('Even numbers in x are:' )
> disp(z)
> disp('Odd numbers in x are:' )
> disp(y)
...

Well, this doesn't address the OP's problem of having floating point
values at all and one doesn't need any loops in Matlab...and, in fact,
your code above works if you simply remove the for and end statements
(and as written does the same thing every time through for the entire
array).

For the OP's question there really isn't an answer other than would have
to round somehow depending on what is/would be wanted.

--


y=x(mod(x,

Tags for 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