How to access a group of bits from a string ?
6 次查看(过去 30 天)
显示 更早的评论
Hello,
I have a problem on how to access a specific range of bits from a given string
Consider I have a 20 bit string,I want to access the bit 1-6 and 7-12 and then 13-20. How do I do it in matlab? please help thanks in advance
Eg: X = 0 0 0 0 0 0 1 1 1 0 0 1 1 1 1 1 0 0 0 0
I want the output as
x1= 0 0 0 0 0 0
x2= 1 1 1 0 0 1
x3= 1 1 1 1 0 0 0 0
I have no idea how to do it.Please help. thanks in advance
0 个评论
采纳的回答
Image Analyst
2015-1-27
It's a little ambiguous, but how about just indexing or subtracting '0':
X = '0 0 0 0 0 0 1 1 1 0 0 1 1 1 1 1 0 0 0 0'
% Get rid of any spaces
X(X==' ') = []
% Extract the three substrings
x1 = X(1:6)
x2 = X(7:12)
x3 = X(13:end)
% Convert to a numerical data type.
doubleX = X - '0'
In the command window:
X =
0 0 0 0 0 0 1 1 1 0 0 1 1 1 1 1 0 0 0 0
X =
00000011100111110000
x1 =
000000
x2 =
111001
x3 =
11110000
doubleX =
0 0 0 0 0 0 1 1 1 0 0 1 1 1 1 1 0 0 0 0
I don't see why cell arrays would be necessary unless I misunderstood something.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!