How can I convert binary vector to a single binary number?

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 Commento

Could you show an example of input array and the output you want to get?

Accedi per commentare.

 Risposta accettata

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 Commenti

Yeah, the idiom is convenient...
nalja mot
nalja mot il 27 Ago 2018
Modificato: nalja mot il 27 Ago 2018
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
nalja mot
nalja mot il 27 Ago 2018
Modificato: nalja mot il 27 Ago 2018
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!!
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.
nalja mot
nalja mot il 27 Ago 2018
Modificato: nalja mot il 27 Ago 2018
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.

Accedi per commentare.

Più risposte (0)

Richiesto:

il 26 Ago 2018

Modificato:

dpb
il 29 Ago 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by