bin2gary: Conversion to char from logical is not possible

10 visualizaciones (últimos 30 días)
ali ayat
ali ayat el 16 de En. de 2022
Comentada: ali ayat el 17 de En. de 2022
hello
i need to convert binary to gary. i did my search and i found this:
function g = bi2gray( b )
g(:,1) = b(:,1);
for i = 2:size(b,2)
g(:,i) = xor( b(:,i-1), b(:,i) );
end
return;
but when i give it a binary number like this:
x=10;
x1=dec2bin(x);
bi2gray(x1)
this error is occured:
Conversion to char from logical is not possible.
Error in bi2gray (line 15)
g(:,i) = xor( b(:,i-1), b(:,i) );

Respuestas (1)

DGM
DGM el 16 de En. de 2022
Editada: DGM el 16 de En. de 2022
The output of dec2bin() is a character array. If you want to do logical operations on it, you have to convert it first.
x = 10;
x1 = dec2bin(x); % this is a char vector
x1 = x1 == '1'; % this is a logical vector
output = bi2gray(x1)
output = 1×4 logical array
1 1 1 1
That said, I'm not sure how this is supposed to convert binary to gray. Is the output supposed to be the same as the input (decimal 10)? If so, why not just do:
x = 10;
x1 = dec2bin(x); % this is a char vector
output = bin2dec(x1)
output = 10
Bear in mind that if you're doing this with images, the output is now improperly-scaled for its class (double), and so it won't be handled correctly by imshow() or imwrite() until it is rescaled or recast appropriately.
function g = bi2gray( b )
g(:,1) = b(:,1);
for i = 2:size(b,2)
g(:,i) = xor( b(:,i-1), b(:,i) );
end
end
  3 comentarios
DGM
DGM el 16 de En. de 2022
Oh Gray code. Yeah I thought you were trying to convert an image to a grayscale representation.
ali ayat
ali ayat el 17 de En. de 2022
Yeah! i keep searching and i found "binaryVectorToDecimal()". & thanks to your hint about conversion of char to logical array and then converting binary vector to decimal, the problem solved!

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by