How to group and convert to decimal?

1 Ansicht (letzte 30 Tage)
Sharen H
Sharen H am 4 Feb. 2013
The output of r is 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1
I want to group every 8 digits and convert to decimal
Please help
  4 Kommentare
Sharen H
Sharen H am 4 Feb. 2013
r() function is a double value
Image Analyst
Image Analyst am 4 Feb. 2013
Bearbeitet: Image Analyst am 4 Feb. 2013
You mean an "array" of doubles is returned by r(), like this [0, 0, 0, 1, 0, 1, 0, etc.], not just a single double like you implied. So, it's like what Azzi assumed.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 4 Feb. 2013
x=[ 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1]
x=num2str(x)
x(x==' ')=[];
y=reshape(x,[],8);
out=bin2dec(y)
  3 Kommentare
Azzi Abdelmalek
Azzi Abdelmalek am 4 Feb. 2013
Ok, there is an error in reshape function
x=[ 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1]
x=num2str(x)
x(x==' ')=[];
y=reshape(x,8,[])';
out=bin2dec(y)
Sharen H
Sharen H am 4 Feb. 2013
Thanks a lot....

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Jan
Jan am 4 Feb. 2013
Bearbeitet: Jan am 4 Feb. 2013
Converting from a double vector to a string and back to a double inside bin2dec is not efficient. Better convert from binary to decimal values directly:
x = [ 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1];
d = 2 .^ (length(x)-1:-1:0) * x(:);

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by