Borrar filtros
Borrar filtros

Convert logical variable into integer number

417 visualizaciones (últimos 30 días)
Guilherme Roberto
Guilherme Roberto el 8 de Mzo. de 2016
Respondida: Marcelo Moraes el 10 de En. de 2022
Hi! I think my problem is very simple. I have some boolean expressions in my code, which the results will always be 1 or 0 (true or false). Here are some examples:
data=data+(-1*(~mat(i)&&mat(j)&&mat(k)))||(~mat(i)&&~mat(j));
or
data=data+(~mat(i));
I have to convert these logicals 1s and 0s to int values. I've tried using abs() but it is not working.
Btw, I'm on R2015b.
I appreciate the help.
Thanks!

Respuesta aceptada

Ced
Ced el 8 de Mzo. de 2016
You can usually use logicals and numerical values interchangeably in matlab (for most operations), no need to convert them. But If you do, you can cast them e.g. using int8
a = logical(0); % a is a logical
b = a; % b is a logical
c = int8(a); % c is an int8
The same goes for other datatypes (int16, int32, double, ... ), see datatypes for details.
  2 comentarios
Jim Morgenstern
Jim Morgenstern el 30 de Mzo. de 2017
Actually this did not work for me ... which may be a bug in Matlab.
I have a logical array: E = uint8(zeros( ..., )); vcl = edge (A, 'canny', ...); vv = uint8(vcl); E = E + uint8(vcl);
when i do imtool(vcl), i see the pixels with edges. when i do imtool(E) it is all zero. and also imtool(vv) is all zero. I changed uint8 to double and it works correctly. possible bug?
Stephen23
Stephen23 el 21 de Dic. de 2021
Editada: Stephen23 el 21 de Dic. de 2021
"I changed uint8 to double and it works correctly. possible bug?"
Not at all. MATLAB assumes that UINT8 image data ranges from 0-255 (i.e. the entire 8-bit range) whereas DOUBLE image data ranges from 0-1 (i.e. normalized). This is clearly explained in the MATLAB documentation:

Iniciar sesión para comentar.

Más respuestas (2)

Kehn Wu
Kehn Wu el 21 de Dic. de 2021
You could try sum()

Marcelo Moraes
Marcelo Moraes el 10 de En. de 2022
In HDC Coder you can use the block
HDL CODER => LOGIC AND BIT OPERATIONS => BIT CONCAT

Community Treasure Hunt

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

Start Hunting!

Translated by