How to alter a bit

1 回表示 (過去 30 日間)
barath manoharan
barath manoharan 2023 年 2 月 9 日
コメント済み: Jan 2023 年 2 月 14 日
iam having a 5*1 cell. Now each cell contains two or more elements.I need to alter a single different bit between two elements. The first row in cell has two elements which differ by a single bit. So the output is expected to be 10010. The second row in cell has two elements which differ by a single bit. So the output is expected to be 11000. The third row consists of five elementsnow each element has to be compared against the remaining elements. first element and last element in 3rd row differs by one bit so the expected output is 10111 similarly 2nd element and last element differs by 1 bit so the expected output is 10111. the remaining 2 elements in 3rd row cannot be merged. 3rd row in the expected output has elements 10111,1100,1011. similar procedure has to be followed for remaining two rows in the cell. Thank you in advance.
For Example:
A =[ [10010;10000]
[1000;11000]
[111;10011;1100;1011;10111]
[1010;11010]
[1111;1110] ]
Expected Output:
B = [ [ 10010]
[11000]
[10111;1100;1011]
[11010]
[1111] ]
  6 件のコメント
barath manoharan
barath manoharan 2023 年 2 月 13 日
YES @Jan. Actually the above mentioned binary values represent the corresponding decimal numbers.
Jan
Jan 2023 年 2 月 14 日
@barath manoharan: Now I've lost the overview completely. I have no idea, what the format of your inputs is. after discussing this important detail for 5 days now, I assume, that I cannot help you.

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

回答 (1 件)

Steven Lord
Steven Lord 2023 年 2 月 9 日
I agree with Jan that the actual format of your data is not clear to me. In the first block of code below A does not contain the values whose binary representation are 10010, 10000, etc.
A =[ [10010;10000]
[1000;11000]
[111;10011;1100;1011;10111]
[1010;11010]
[1111;1110] ]
A = 13×1
10010 10000 1000 11000 111 10011 1100 1011 10111 1010
If you're entering your data in the Command Window or in a MATLAB program file you can make them the values with those binary representations using the 0b prefix.
B = [0b10010, 0b10000]
B = 1×2
18 16
Once you've done that you can use the bit-wise operations functions.
bitget(B, 5)
ans = 1×2
1 1
bitget(B, 2)
ans = 1×2
1 0
bitget(B(1), 5:-1:1)
ans = 1×5
1 0 0 1 0
C = bitset(B, 3, 1)
C = 1×2
22 20
Afterwards you could use dec2bin.
dec2bin(B)
ans = 2×5 char array
'10010' '10000'
dec2bin(C) % These differ from the display of B due to the bitset call
ans = 2×5 char array
'10110' '10100'

カテゴリ

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

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by