How can I convert binary vector to a single binary number?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Share a link to this question
Hi
could you please help me? I want to convert a vector which consists binary elements to a single binary number? i want to give 010110 from [0 1 0 1 1 0];(it is necessary for me to keep the first zero) and i want to keep format of number as a binary element because i want to compare the output with other binary numbers
1 Comment
madhan ravi
on 26 Aug 2018
Could you show an example of input array and the output you want to get?
Accepted Answer
dpb
on 26 Aug 2018
The representation as 010110 visually is immaterial; to do that it will have to be character:
b= [0 1 0 1 1 0];
sprintf('%d',b)
ans =
'010110'
For internal comparison to values, just convert to decimal--unfortunately, Matlab doesn't have a scan format string for binary, use
v=uint8(bin2dec(sprintf('%d',b))) % convert to value
v =
22
To compare/find bits...
bitget(v,1:6)
ans =
1×6 uint8 row vector
0 1 1 0 1 0
8 Comments
Walter Roberson
on 26 Aug 2018
bin2dec(char(b+'0'))
dpb
on 26 Aug 2018
Yeah, the idiom is convenient...
thanks a lot for your answers
sorry but unfortunately these answers cannot satisfy my purpose because as i told i want to get a single number and with these instructions i'll get:
ans =
010110
ans(1)=0
ans(2)=1
...
but i want to get ans(1)=010110
more over i want to compare result with binary number and because my process is very complicated, should not do binary to decimal changes for each iteration and should summarize the code if possible!!
dpb
on 27 Aug 2018
ADDENDUM [Moved from Answer by dpb for compactness in thread]
let me explain problem with details:
i have a vector of integer number(but this vector consist just 0 and 1):D=[1 1 1 0 1 0 0 1 1 0 1]
i want to extract some elements of it as a single number: A=D(7:11)=...=01101
lots of instructions exist to get a single number as A=D(7:11)=...=1101
for example:
x = [0 0 1 0 1 0 1];
p = flipud([1; cumprod(repmat(10, size(x, 2) - 1, 1))]);
y = x * p;
this instruction results a single integer number as:
y=10101 but i want to get 0010101 (as a binary single number albeit if i get exactly a single number 0010101 it is not important that it is binary or integer because i want to compare it with a constant that can define it binary or integer, but i think it is impossible that i get 0010101 as integer!!)
Again, you're mixing the internal representation with the visual display of the value; '0010101b' --> 21 as a value, the only way to represent it visually as the list of set bits is a string of some sort; char() array, cell string or string class.
The operation above is rather clever to build a decimal number which has powers of 10 at the bit positions desired to visually simulate the appearance but it is, of course, 10,101 as far as the actual value and still isn't by default going to be visually displayed with leading zeros unless you write them...
num2str(y,'%07d')
ans =
'0010101'
but again, you're back to the character representation to be able to see the leading zeros on the screen.
The internal value however, is unique in either form and tests for equality can be done with the native representation.
Or, the alternative would be to use the string representation throughout, with strings one can do something similar (using the Matlab idiom Walter pointed out)
D=[1 1 1 0 1 0 0 1 1 0 1];
A=D(7:11);
string(char(A+'0'))
ans =
"01101"
which, while not an integer, is a single entity that can be used in comparisons with "==" operator for matching.
BUT, you will have to ensure you build the strings with the same lengths including leading zeros.
It probably makes more sense to use native storage as numeric value internal and simply have external routines to display results as binary strings where needed for visualization.
yes, its ok
thank you very much
You're welcome, glad to (try to) help... :)
Matlab doesn't have the facility; Forth has the system constant BASE which is very useful for such shenanigans; one could write
21 2 BASE ! . DECIMAL
and the value of 21 would be displayed on the terminal in base 2 then the working base returned to base 10. Most define
: BINARY 2 BASE !;
as a "syntactic sugar" word (aka Matlab function) and then above could be shortened to
21 BINARY . DECIMAL
Matlab doesn't have the facility to change working base arbitrarily for the input parser, however, you've got to do it by transforming results.
ADDENDUM
Of course, while the above is very succinct from the user standpoint and an extremely handy facility, it doesn't actually change the underlying fact that the value '21' is still stored as twos-complement integer whether BASE is set at 2 or 10 or 16 or 37 (a value handy to use for encoding simple hash table keys or the like); the same cast operations between internal representation and external display are present, they're just handled "under the hood" by builtin logic within the FORTH engine as part of the core functionality.
Chuck Moore built this in when he invented Forth as he was doing instrumentation and control where bit operations interacting with hardware bit registers was a key component of the application so being able to use a flexible BASE was fundamental to convenience. Matlab, being designed primarily as the MATrix LABoratory didn't have such low-level operations in mind to need a similar facility.
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
See Also
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)