how to create binary matrix in matlab
古いコメントを表示
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 件のコメント
James Tursa
2019 年 4 月 25 日
Please give a short example of what you mean by decimal and binary matrix, and what the conversion would be for this example.
Abdelmalek Benaimeur
2019 年 4 月 25 日
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
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
採用された回答
その他の回答 (2 件)
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
2019 年 4 月 25 日
Abdelmalek Benaimeur
2019 年 4 月 25 日
James Tursa
2019 年 4 月 25 日
編集済み: James Tursa
2019 年 4 月 25 日
How is A in your example above a double? Do you mean you want to input it as written above where 0010 would be "ten" as a double, but then turn around and re-interpret it as a binary character array? I.e., something like this?
bin = reshape(sprintf('%04d',A),4,[])'
Abdelmalek Benaimeur
2019 年 4 月 25 日
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
2019 年 4 月 25 日
0 投票
1 件のコメント
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
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!