Replace string values with another string value.

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.

 採用された回答

dpb
dpb 2019 年 3 月 4 日

1 投票

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 件のコメント

Kaushik Vasanth
Kaushik Vasanth 2019 年 3 月 5 日
編集済み: dpb 2019 年 3 月 5 日
I am still a bit confused on how this works to get a desired output.
I have atttached the data that i need to convert it into 5 bit binary form.
My values only range from -8 to 8 throught the file.
So could you please tell me how to do the same.
I was thinking to just assign values to the data.
For eg:
if i have 2 then i assign '00010' and if it is -1 then I assign '11111'.
I have also attached the code so you can have a look
% Load the values
a = load('adc.csv');
a1=a(:,[1]);
% figure(1);
% plot(a1);
%% Converting into integers from +8 to -8
y = a1 / 0.0625; to convert into integers
y1 = y';
y1 = num2str(y1);
y1 = split(y1);
r = regexprep(y1,{'0','1','2','3','4','5','6','7','8','-1','-2','-3','-4','-5','-6','-7','-8'},{'00000','00001','00010','00011','00100','00101','00110','00111','01000','11111','11110','11101','11100','11011','11010','11001','11000'});
c = split(r);
c = cell2table(c);
writetable(c,'try.txt')
I have attached the output file and you can see clearly that for -ve values it still takes the value of +ve1 and appends -ve sign at the front
dpb
dpb 2019 年 3 月 5 日
編集済み: dpb 2019 年 3 月 5 日
Once you have your integers just use the anonymous function to get the twos-complement text representation -- or rewrite it into an m-file if you prefer.
It will return your twos-complement string array directly from the input--there's no reason for any character substitution at all. You could cast the output to cellstr() or the new(ish) string() to make handling the strings a little more convenient as an array of char() requires 2D indexing since each character is just an element in a 2D array instead of an entity.
>> x=xlsread('KV.xlsx');
>> n=fntwoscomp(x,5);
>> n(1:5,:)
ans =
5×5 char array
'00000'
'00000'
'00000'
'00001'
'00001'
>> n(end-10:end,:)
ans =
11×5 char array
'11100'
'11101'
'11101'
'11100'
'11101'
'11100'
'11101'
'11101'
'11101'
'11101'
'11101'
>>
function s=twoscomp(v,n)
% returns twos-complement string aka dec2bin except handles negative integers as well
% TWOSCOMP(V) returns the binary representation of V as a character vector.
%
% DEC2BIN(D) returns the binary representation of D as a character vector.
% D must be a non-negative integer. If D is greater than flintmax,
% DEC2BIN might not return an exact representation of D.
%
% DEC2BIN(V,N) produces a binary representation with at least N bits.
if nargin<2
n=1;
else
if ~(isnumeric(n) || ~isscalar(n)
error(message('twoscomp:NBits Arg'));
end
n = round(double(n));
end
s=dec2bin(mod(v,2.^n),n);
end
Kaushik Vasanth
Kaushik Vasanth 2019 年 3 月 6 日
Hi,
Thanks a lot for this !!!
>> x=xlsread('KV.xlsx');
>> n=fntwoscomp(x,5);
>> n(1:5,:)
This really worked perfect and reduced lines of code.
Appreciate it a lot :)
dpb
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...
Kaushik Vasanth
Kaushik Vasanth 2019 年 3 月 7 日
Yes, thats so true. The right way to commuicate the issue is a bigger problem :)
Thanks a lot for your feedback :)

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by