Replace string values with another string value.
1 次查看(过去 30 天)
显示 更早的评论
I am trying to assign value for the string data.
for eg :
0 = 00000
1 = 00001
-1 = 11111 (2's complement)
I have the data in the format 0 0 0 1 1 2 0 -1 -3 and so on. It ranges from -8 to 8 . And now I want to assign 5bit binary equivalent for the same.
But when I use "regexprep(y1,{'0','1','2','3'},{'00000','00001','00010','00011})" I get the right values for the positive numbers but when I try to assign the same for negative scale it takes the '-' sign and displays the value of 1. For eg: if my input is regexprep(y1,{'-1'},{'11111'}) but i get output as '-00001'. I have attached the pictures in the same.
Looking forward to your support.
0 个评论
采纳的回答
dpb
2019-3-4
fntwoscomp=@(v,n) dec2bin(mod(v,2.^n),n);
>> fntwoscomp(-3:3,5)
ans =
7×5 char array
'11101'
'11110'
'11111'
'00000'
'00001'
'00010'
'00011'
>>
5 个评论
dpb
2019-3-6
Glad to help...sometimes one has to revisit the idea of what the problem is one is trying to solve! :)
If the solution solves the problem, please go ahead and Accept the answer so it can be seen to be closed by others looking either to help or that might actually search for similar topic...
更多回答(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!