how to define binary matrix in matlab?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
let's suppose that i have this matrix
A=[0001
0010;
0011;
0100;
0101]
how can i define it to matlab so that it will be reconized as binary matrix and not double ?
採用された回答
James Tursa
2019 年 4 月 25 日
編集済み: James Tursa
2019 年 4 月 25 日
If you mean that you type in the A matrix exactly as above, but want to reinterpret the decimal digits as binary digits, then maybe something like this:
>> A=[0001
0010;
0011;
0100;
0101]
A =
1
10
11
100
101
>> bin = reshape(sprintf('%04d',A),4,[])'
bin =
0001
0010
0011
0100
0101
If you wanted that as a logical matrix instead of a char matrix, then
bin = (bin == '1')
7 件のコメント
Abdelmalek Benaimeur
2019 年 4 月 25 日
bin = reshape(sprintf('%04d',A),4,[])'
returns char_array i want the result in a matrix
James Tursa
2019 年 4 月 25 日
See edited answer.
If you're looking for a numeric matrix with 0 placeholders, that's not going to happen. Even the dec2bin() function outputs a char array.
dec2bin([1:5])
ans =
5×3 char array
'001'
'010'
'011'
'100'
'101'
Your initial matrix "A" is already in numeric form and represents the same binary as if it had place holders.
A =[
1
10
11
100
101];
bin2dec(num2str(A))
ans =
1
2
3
4
5
bin = ['0001'
'0010'
'0011'
'0100'
'0101'];
bin2dec(bin)
ans =
1
2
3
4
5
Abdelmalek Benaimeur
2019 年 4 月 25 日
Abdelmalek Benaimeur
2019 年 4 月 25 日
please last question
see this code
A =
6 2 2
3 5 1
7 4 6
5 8 3
6 8 9
3 7 9
>> bin = dec2bin(A,4)
bin =
18×4 char array
'0110'
'0011'
'0111'
'0101'
'0110'
'0011'
'0010'
'0101'
'0100'
'1000'
'1000'
'0111'
'0010'
'0001'
'0110'
'0011'
'1001'
'1001'
>> bin=(bin=='1')
bin =
18×4 logical array
0 1 1 0
0 0 1 1
0 1 1 1
0 1 0 1
0 1 1 0
0 0 1 1
0 0 1 0
0 1 0 1
0 1 0 0
1 0 0 0
1 0 0 0
0 1 1 1
0 0 1 0
0 0 0 1
0 1 1 0
0 0 1 1
1 0 0 1
1 0 0 1
>> bin2dec(num2str(bin))
ans =
6
3
7
5
6
3
2
5
4
8
8
7
2
1
6
3
9
9
how can i obtain the original matrix A in this case?
James Tursa
2019 年 4 月 25 日
If you know the size of the original A is 6x3, then simply reshape
>> reshape(bin2dec(num2str(bin)),6,3)
ans =
6 2 2
3 5 1
7 4 6
5 8 3
6 8 9
3 7 9
If you don't know the size of the original A, then you are stuck because that information is not contained in the variable bin. You would need some other way to know what the variable size should be.
Abdelmalek Benaimeur
2019 年 4 月 25 日
believe me you are the best
this is for my graduation projet i will add your name as a reference
wish you all the best
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Matrices and Arrays についてさらに検索
参考
2019 年 4 月 25 日
2019 年 4 月 25 日
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
