How to alter a bit

조회 수: 2 (최근 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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by