Can anybody guide me how to do this right in matlab.
Actually How I can distinguish odd from even from numbers (0
to 8 with if and else)?
ex. if x==1 or x==3 or x==5 or x==7 do that else if x==0 or
x==2 or x==4 or x==6 or x==8 do that.
if (x==1 or x==3 or x==5 or x==7)
.
.%do this
.
elseif (x==0 or x==2 or x==4 or x==6 or x==8)
.
.%do this
.
end;
Kindly suggest me how to do it..
Thanks in advance
>Can anybody guide me how to do this right in matlab.
>Actually How I can distinguish odd from even from numbers (0
>to 8 with if and else)?
>ex. if x==1 or x==3 or x==5 or x==7 do that else if x==0 or
>x==2 or x==4 or x==6 or x==8 do that.
first = @(v) v(1);
if first(factor(x)) - 2
%odd
else
%even
end
--
"I will speculate that [...] applications [...] could actually see a
performance boost for most users by going dual-core [...] because it
is running the adware and spyware that [...] are otherwise slowing
down the single CPU that user has today" -- Herb Sutter
The explicit comparison is not necessary since mod(x,2) is
zero or one for integer x:
if mod(x,2)
disp('odd')
else
disp('even')
end
True, but I thought I'd do it that way to emphasize what's going on, so he/she would understand it better. :)
> Actually How I can distinguish
> odd from even from numbers...
one of the many solutions
x=-4:4;
tf=bitget(abs(x),1)~=0;
odd=x(tf)
% odd = -3 -1 1 3
even=x(~tf)
% even = -4 -2 0 2 4
note: do NOT use rem/mod-stuff! it comes with unnecessary
FP-issues, which have been shown several times here at
CSSM...
us
What does this mean? Can you refer me to a post? (I searched for "rem mod fp" and nothing shows up.)
Thanks.
> Can you refer me to a post
one of the latest can be found here
% on
http://groups.google.com/group/comp.soft-sys.matlab/topics
% search for
even numbers bitand rem
% look for instance at the one posted on aug 24, 2007
us
> tf=bitget(abs(x),1)~=0;
Interesting, bitget() is not documented to work with doubles
(at least as of R2007a) but it does, for bits 1 to 52.
I'll file a documentation request. (Now filed.)
--
"The quirks and arbitrariness we observe force us to the
conclusion that ours is not the only universe." -- Walter Kistler
> Actually How I can distinguish odd from even from numbers (0
> to 8 with if and else)?
I've always used
if floor(number)==number, %then even
else % odd
end
> I've always used
> if floor(number)==number, %then even
> else % odd
> end
now - i can better appreciate the results of your last
science paper...
x=1:5;
floor(x)==x
% ans = 1 1 1 1 1
just a thought...
us
???
> x=1:5;
> floor(x)==x
> % ans = 1 1 1 1 1
I guess that's what I call 'vector', which usually is a
little different from 'number'.
The topic quest was
> How I can distinguish odd from even from numbers (0
to 8 with if and else)?
If I have a vector, I'd just use the
floor(vector(i))==vector(i) inside a for cycle.
What? How can that possibly work?
floor(5)==5
is true. So is 5 even?
As for not using mod()... I still don't really see the big deal. Why would you test for even/odd on non-integers anyway?
> As for not using mod()... I still don't really see the
> big deal....
> Why would you test for even/odd on non-integers anyway...
well, think more, think computer...
x=1:5;
mod(x,2)
% ans = 1 0 1 0 1
% fine, now
x=realmax+(-5:0);
mod(x,2)
% ans = 0 0 0 0 0 0 % allow me to put an ! here
x=x/1e100;
mod(x,2)
% ans = 0 0 0 0 0 0 % ... and again
% because of the uncertainty
eps(realmax)
% ans = 1.9958e+292
% whereas
bitand(x,1) % -or- bitget(...)
%
% will show you right away that you will not
% get a real answer...
%{
??? Error using ==> bitand
Exceeded value of bitmax.
%}
better believe me, this naive mod/rem approach has been a
bigger issue for many a people than you might like... (not
everybody's even/odd problem is in the range of [-10/10])
us
> I guess that's what I call 'vector', which usually is a
> little different from 'number'.
> The topic quest was
> > How I can distinguish odd from even from numbers (0
> to 8 with if and else)?
> If I have a vector, I'd just use the
> floor(vector(i))==vector(i) inside a for cycle...
1) all of this does not make sense...
2) if i'd ever use your number-approach, i'd end up with
this nonsense
% create a bunch of even/odd numbers
vector=0:5;
for i=1:numel(vector)
r=floor(vector(i))==vector(i); % your snipplet...
disp(sprintf('v(%1d) = %2d: %2d',i,vector(i),r));
end
%{
v(1) = 0: 1 % your even
v(2) = 1: 1 % your even
v(3) = 2: 1 % your even
v(4) = 3: 1 % ...
v(5) = 4: 1
v(6) = 5: 1
%}
us
us, you make some good points, but is realmax an integer?
Please forgive me for my lack of years of Matlabing (I am
still a learner!), but what is wrong with this:
x = double(intmax)+(-10:10);
mod(x,2)' % Alternating 1 0 1....
x2 = round(realmax) + (-10:10);
mod(x,2)' % Alternating 1 0 1....
Check this conclusion please:
So as long as we sure we are using integers, the mod method
works.
> ...but is realmax an integer...
> x = double(intmax)+(-10:10);
> mod(x,2)' % Alternating 1 0 1....
> x2 = round(realmax) + (-10:10);
> mod(x,2)' % Alternating 1 0 1....
> Check this conclusion please:
> So as long as we sure we are using integers, the mod
> method works...
hi matt, that is JUST the point!
realmax is: the largest pos FP number
most CSSMers are living in this realm of bits and bytes
intmax is: the largest pos INT number
most CSSMers don't care about the real world
now, as long as people CHECK for the validity of their
numbers, ie, abs(number) <= intmax, NOTHING would be wrong
with the unholy MOD/REM critters...
however, people tend NOT to do so, and tend to have
(apparent) false ML results, and tend to accuse of ML being
buggy, and (then) tend to whine about this on CSSM...
all i like to get across is this pedestrian message:
- unless you cognitively check that your number can truly
be represented by a unique bit-pattern (ie, int), you
should NOT naively use REM/MOD for your calculations...
- needless to say that any suchy CHECK requires additional
code in your snippet, which makes ML life (even?) more
cumbersome...
- needless to say that most people (at first) do NOT care
about this FP issue...
- needless to say that the BIT... family of stock functions
takes care of this - by a nasty error message...
just another though
as ever, best
urs
> just another though
should read
just another thought
us
Point well taken, thanks us. ;)
Angelina
Hi Marie,
I have one other method to solve your problem. It does not
use if else end but I think it is very readable.
switch x
case{0,2,4,6,8}
str = 'Even Set';
case{1,3,5,7}
str = 'Odd Set';
otherwise
str = 'Otherwise';
end
Good Luck,
Donn
:D
You're absolutely right; I forgot a piece:
if floor(number/2) == number/2, % then it's even
else % it's odd
Sorry guys :)
???
By definition every even number is divisible by 2.
Checking this is sufficient to identify even numbers.
Where's the problem?
It's so easy I actually forgot the most stupid part, when I
answered this topic: dividing the number by 2 prior to
verify it.
> You're absolutely right; I forgot a piece:
> if floor(number/2) == number/2, % then it's even
> else % it's odd
>> floor(inf/2) == inf/2
ans =
1
Guess inf must be even then...
>> floor((inf+1)/2) == (inf+1)/2
ans =
1
And inf+1 is even too...
> By definition every even number is divisible by 2.
> Checking this is sufficient to identify even numbers.
> Where's the problem?
>> floor(i/2) == i/2
ans =
0
>> floor((i+1)/2) == (i+1)/2
ans =
0
So is i even or odd?
> You're absolutely right; I forgot a piece:
> if floor(number/2) == number/2, % then it's even
> else % it's odd
> Sorry guys :)
%{
BITGET
BITAND
%}
n=realmax+(-5:0); % a possible number a user may have
floor(n/2)==n/2
% ans = 1 1 1 1 1 1
us
% A starting point to naively look for the
% maximum value for which rem/mod works.
num = bitmax - 10000;
tf1 = rem(num,2);
tf2 = rem(num+1,2);
while tf1 ~= tf2
num = num+1; % Find that max value...
tf1 = rem(num,2);
tf2 = rem(num+1,2);
end
num
epsnum = eps(num) % This is interesting.
epsltnum = eps(num-1) % Ah-ha
% As us was saying, it is no coincidence that we get:
num2str(bitget(num-1,1:52)) % Here we are maxed out.
try
bitget(num,1:52) % Here we get an error
catch
disp(lasterr)
end
% and also
num - bitmax
% All numbers > bitmax are even (with rem/mod)!!!
% Lesson: at least with the bitget test,
% we get an error, better than false output!
x = uint8(1);
x= a number of your choice
test=x/2;
decide=isinteger(test);
if decide=1 (then x was a even number)
do something
end
if decide=0 (then x was an odd number)
do something
end
I'm not sure why you think the many previous posts provided no answer. Regardless, your proposal doesn't work, as this example shows
>> x=uint8(1);
>> x=4;
>> test=x/2
test =
2
>> decide=isinteger(test) %concludes that the choice x=4 was odd
decide =
0
A modification of your idea that would work is as follows
>> x=4; decide=isequal(x/2,uint64(x/2)),
decide =
1
>> x=5; decide=isequal(x/2,uint64(x/2)),
decide =
0
i meant a simple answer
add
test=uint8(1); to the start
Nope. It still doesn't work. Nor does it seem simpler than previous proposals, as it requires at least 5 lines of code
>> test=uint8(1); x=uint8(1); x=4; test=x/2;
>> decide=isinteger(test)
decide =
0
"james " <james.fr...@googlemail.com> wrote in message
news:ilapb1$nic$1...@fred.mathworks.com...
>
> a dead thread without an answer
>
> x = uint8(1);
>
> x= a number of your choice
>
> test=x/2;
>
> decide=isinteger(test);
ISINTEGER probably doesn't do what you think it does.
http://www.mathworks.com/help/techdoc/ref/isinteger.html
It determines if the input is of an integer _data type_, not if the value is
an integer _value_.
isinteger(double(5)) % false since double is not an integer data type
Take a look at:
x = uint8(5);
test = x/2;
isinteger(test) % true because the result of dividing a uint8 by a double
scalar is of class uint8
--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com
I always like to go for code reusability and readability, so if I were to do it I would create a function as follows:
function result = isEven(this)
result = mod(this,2);
Then my code would look like:
if isEven(theValue)
% --- do something
else
% --- do something else
end
I hope it helps
Riccardo