How to alter a bit
Show older comments
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 Comments
You mention a cell, but your example is no cell. The type of the inputs is unclear: 10010 is a decimal value. What do you call "a bit" then? Are the inputs really decimal values or are you talking of binary cell strings like
A = {{'10010'; '10000'}, ...
{'1000'; '11000'}, ...
It is not efficient, if the readers guess, what your inputs are.
What is the general rule to select the output, if the values differ by 1 bit only? Why is is 10010 in the first case and not 10000?
barath manoharan
on 10 Feb 2023
Steven Lord
on 10 Feb 2023
The obtained value is in binary representation.
No, it's not. The first element of your first cell is not 18. It is ten thousand and ten.
How are you generating or reading this data into MATLAB?
Jan
on 10 Feb 2023
@barath manoharan: Prefer to post data in a format, which can be used tu write an answer with copy&paste. A screenshot is not useful and letting the readers type the inputs is not efficient also.
The input data are decimal, as Steven has written already. You did not clarify yet, which of the two numbers is chosen, if the differ by 1 bit only. Therefore neither the input nor the wanted procedure has been explained uniquely.
barath manoharan
on 13 Feb 2023
Jan
on 14 Feb 2023
@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.
Answers (1)
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] ]
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]
bitget(B, 5)
bitget(B, 2)
bitget(B(1), 5:-1:1)
C = bitset(B, 3, 1)
dec2bin(B)
dec2bin(C) % These differ from the display of B due to the bitset call
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!