how to create binary matrix in matlab
123 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Abdelmalek Benaimeur
el 25 de Abr. de 2019
Editada: Erick Huelsz
el 11 de Mayo de 2022
hello,
I'm wondering how to create a binary matrix in matlab
and how to convert a matrix from decimal to binary and from binary to decimal
4 comentarios
Erick Huelsz
el 11 de Mayo de 2022
I am sure there must be an easier way, but so far I managed to do this with the info I found:
D=[1,2,3,4,5]
l=length(D)
for i=1:l
bin = '0000000000';
bin = [bin dec2bin(D(i))];
b=(bin(end-l+1:end))=='1';
M(i,1:l)=b;
end
M
Erick Huelsz
el 11 de Mayo de 2022
Editada: Erick Huelsz
el 11 de Mayo de 2022
yeah, there was a little easier way:
D2=[1,2,3,4,5]
l=length(D2)
for i=1:l
b=dec2bin(D2(i))
b=b-48
M2(i,l+1-length(b):l)=b
end
Respuesta aceptada
KALYAN ACHARJYA
el 25 de Abr. de 2019
Editada: KALYAN ACHARJYA
el 25 de Abr. de 2019
Second part, this is broad way to express-
Here I trying one example, please look
>> a=randi(5,5)
a =
1 5 2 5 4
5 5 4 4 4
3 3 2 2 5
5 2 4 4 5
4 1 1 2 2
>> binary=(a<3)
binary =
1 0 1 0 0
0 0 0 0 0
0 0 1 1 0
0 1 0 0 0
0 1 1 1 1
Here this is logical expression
If you are talking to decimal to binary, as processor do the computaion in binary values only, all decimal elements convert to binary as general. I hope you know the procedure.
Or check here
0 comentarios
Más respuestas (2)
James Tursa
el 25 de Abr. de 2019
Editada: James Tursa
el 25 de Abr. de 2019
>> dec=[1;2;3;4;5];
>> bin = dec2bin(dec,4)
bin =
0001
0010
0011
0100
0101
>> bin2dec(bin)
ans =
1
2
3
4
5
5 comentarios
James Tursa
el 25 de Abr. de 2019
Editada: James Tursa
el 25 de Abr. de 2019
I assume by binary you really mean logical. Then simply
bin = (bin == '1')
Abdelmalek Benaimeur
el 25 de Abr. de 2019
1 comentario
KALYAN ACHARJYA
el 25 de Abr. de 2019
This is logical representation, all elements of a, which are less than 3 are true (1) and others false. Please response on @James Tursa comment
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!