Jan 5, 2010 #1 N neoflash Advanced Member level 1 Joined Jul 2, 2005 Messages 492 Helped 10 Reputation 20 Reaction score 2 Trophy points 1,298 Activity points 4,759 For example, "101" is a negative binary variable. How to convert it into decimal in matlab?
Jan 5, 2010 #2 J JoannesPaulus Advanced Member level 3 Joined Mar 19, 2008 Messages 773 Helped 235 Reputation 470 Reaction score 138 Trophy points 1,323 Location USA Activity points 5,006 Re: Matlab: Convert a signed binary vector into signed integ Not very straightforward: Code: b='101'; if(b(1)==0) d=bin2dec(b(2:3)); else d=bin2dec(b(2:3))-3; end basically, if the MSB is 1 you have a negative number (2's complement in the example).
Re: Matlab: Convert a signed binary vector into signed integ Not very straightforward: Code: b='101'; if(b(1)==0) d=bin2dec(b(2:3)); else d=bin2dec(b(2:3))-3; end basically, if the MSB is 1 you have a negative number (2's complement in the example).
Mar 4, 2011 #3 F frznchckn Member level 1 Joined Jul 20, 2010 Messages 36 Helped 4 Reputation 8 Reaction score 4 Trophy points 1,288 Location California Activity points 1,521 A somewhat old post, but shouldn't it be something like Code: function [y] = sbin2dec(x) if(x(1)==0) y = bin2dec(x(2:end)); else y = bin2dec(x(2:end)) - 2 ^ (length(x) - 1); end
A somewhat old post, but shouldn't it be something like Code: function [y] = sbin2dec(x) if(x(1)==0) y = bin2dec(x(2:end)); else y = bin2dec(x(2:end)) - 2 ^ (length(x) - 1); end
Feb 28, 2012 #4 S soonerfw Newbie level 1 Joined Dec 11, 2009 Messages 1 Helped 0 Reputation 0 Reaction score 0 Trophy points 1,281 Activity points 1,284 There is a typo in the code, it should be: if (x(1)=='0') % instead of ==0 frznchckn said: A somewhat old post, but shouldn't it be something like Code: function [y] = sbin2dec(x) if(x(1)==0) y = bin2dec(x(2:end)); else y = bin2dec(x(2:end)) - 2 ^ (length(x) - 1); end Click to expand...
There is a typo in the code, it should be: if (x(1)=='0') % instead of ==0 frznchckn said: A somewhat old post, but shouldn't it be something like Code: function [y] = sbin2dec(x) if(x(1)==0) y = bin2dec(x(2:end)); else y = bin2dec(x(2:end)) - 2 ^ (length(x) - 1); end Click to expand...