필터 지우기
필터 지우기

how to create binary matrix in matlab

조회 수: 107 (최근 30일)
Abdelmalek Benaimeur
Abdelmalek Benaimeur 2019년 4월 25일
편집: Erick Huelsz 2022년 5월 11일
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
Erick Huelsz
Erick Huelsz 2022년 5월 11일
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
Erick Huelsz 2022년 5월 11일
편집: Erick Huelsz 2022년 5월 11일
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

댓글을 달려면 로그인하십시오.

채택된 답변

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 4월 25일
편집: KALYAN ACHARJYA 2019년 4월 25일
For the First part of the question please refer to link here (@Walter Roberson's answer)
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.

추가 답변 (2개)

James Tursa
James Tursa 2019년 4월 25일
편집: James Tursa 2019년 4월 25일
>> 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
Abdelmalek Benaimeur
Abdelmalek Benaimeur 2019년 4월 25일
no what i meant is
let's take the same example
dec=[1;2;3;4;5];
the class of this vector is : double
when i do this
bin = dec2bin(dec,4)
i obtain char array like this
bin =
5×4 char array
'0001'
'0010'
'0011'
'0100'
'0101'
my question is when converting from dec to bin
how can i obtain the result in a matrix and not a char array
so that matlab reconize that the elements of this matrix are binary and not double
James Tursa
James Tursa 2019년 4월 25일
편집: James Tursa 2019년 4월 25일
I assume by binary you really mean logical. Then simply
bin = (bin == '1')

댓글을 달려면 로그인하십시오.


Abdelmalek Benaimeur
Abdelmalek Benaimeur 2019년 4월 25일
please could you tell me what is the meaning of a<3 here : binary=(a<3)
  댓글 수: 1
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 4월 25일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by