How can i flip specific bit of an binary array.

17 ビュー (過去 30 日間)
aditya kumar sahu
aditya kumar sahu 2016 年 12 月 12 日
編集済み: James Tursa 2019 年 4 月 5 日
suppose i have a=[1 0 1] and i want to flip the 2nd position bit then i can do by a([2])=~a([2]). but let if a = [1 0 1; 0 1 1; 1 0 0; 1 0 1](large matrix of binary numbers] then , how can a specific bit can be flipped.

採用された回答

James Tursa
James Tursa 2016 年 12 月 12 日
E.g., the bit in the (2,3) position:
a(2,3) = ~a(2,3);
  2 件のコメント
Anmol Shah
Anmol Shah 2019 年 4 月 5 日
I am trying this code but getting the error
"Conversion to char from logical is not possible."
I'm using the below commands
z=(dec2bin(20))
z(1,1) = ~z(1,1);
James Tursa
James Tursa 2019 年 4 月 5 日
編集済み: James Tursa 2019 年 4 月 5 日
The ~ operator is appropriate for logical or numeric variables. For char variables that you know to contain only '0' and '1' characters, you could just do the following:
z(1,1) = '0'+'1'-z(1,1);
The expression on the rhs is double, but will be converted back into char as a result of the indexed assignment on the lhs. If you are not doing an indexed assignment, you may need to wrap the expression in the char( ) function:
char('0'+'1'-z(1,1))

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by