You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Binary value conversion.
1 view (last 30 days)
Show older comments
I have a binary sequence like this :
01011001
01010110
01010010
01001111
01001100
01010010
01011001
How can I convert all the 0 values to -1..? 1 values will be 1 as well.
2 Comments
John D'Errico
on 2 Jan 2021
Please. There was no reason to ask the same question twice.
Noman Abir
on 2 Jan 2021
The network issue. That's why it submitted twice. sorry.
Accepted Answer
Image Analyst
on 2 Jan 2021
mask = yourSequence == 0; % Find all 0s
yourSequence(mask) = -1; % Convert 0s to -1s.
25 Comments
Noman Abir
on 2 Jan 2021
The code is not working. This is a part of a binary sequence i found from an audio signal by using dec2bin command (with sampling, quantization and encoding).
Image Analyst
on 2 Jan 2021
Attach your binary sequence variable in a .mat file.
Noman Abir
on 2 Jan 2021
Here, g is my binary sequence and I want to convert all the 0 values to -1.
Image Analyst
on 2 Jan 2021
Try this:
clc;
clear all;
close all;
bit=8;
levels=256;
Fs=8000;
t=0:1/Fs:(10-1)/Fs;
x = [-0.52, 1.2, 1.65, -1.99, -0.8, 1.96, 1.32, 0.76, 0, 0.01];
% stairs(t,x);
% title('Sample values stairs version figure');
max_x = 2;
min_x = -2;
step=(max_x-min_x)/levels;
lowest_level=min_x+(step/2);
highest_level=max_x-(step/2);
level=lowest_level:step:highest_level;
level_number=ceil((x-min_x)/step);
quantized_value=level(level_number);
encoded = x/quantized_value*level_number;
g = dec2bin(encoded);
g
% Convert from character array to string.
g = string(g)
[rows, columns] = size(g)
% Scan down row by row replacing 0 with -1
for row = 1 : rows
thisRow = g(row)
newRow = strrep(thisRow, "0", "-1")
g(row) = newRow;
end
g
You'll get
g =
10×1 string array
"-11-11111-1"
"11-1-111-1-1"
"111-11-1-11"
"-1-1-1-1-1-1-1-1"
"-11-1-111-1-1"
"111111-11"
Noman Abir
on 2 Jan 2021
It's showing error.
Noman Abir
on 2 Jan 2021
Can you do this with if-else command..? (in for loop).
I have tried this but it's giving me first row valuse from the binary sequence.
I have added the full code, you can check it.
clc;
clear all;
close all;
bit=8;
levels=256;
Fs=8000;
t=0:1/Fs:(10-1)/Fs;
x = [-0.52, 1.2, 1.65, -1.99, -0.8, 1.96, 1.32, 0.76, 0, 0.01];
% stairs(t,x);
title('Sample values stairs version figure');
max_x = 2;
min_x = -2;
step=(max_x-min_x)/levels;
lowest_level=min_x+(step/2);
highest_level=max_x-(step/2);
level=lowest_level:step:highest_level;
level_number=ceil((x-min_x)/step);
quantized_value=level(level_number);
encoded = x/quantized_value*level_number;
z = dec2bin(encoded);
j = 1
for i=1:length(t)
if z(i)=='0';
num(i) = -1
else
num(i) = 1
number = num(i)
j = j+1
end
end
number
Image Analyst
on 2 Jan 2021
Try this if you want a number out instead of a string:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fprintf('Beginning to run %s.m ...\n', mfilename);
bit=8;
levels=256;
Fs=8000;
t=0:1/Fs:(10-1)/Fs;
x = [-0.52, 1.2, 1.65, -1.99, -0.8, 1.96, 1.32, 0.76, 0, 0.01];
% stairs(t,x);
title('Sample values stairs version figure');
max_x = 2;
min_x = -2;
step=(max_x-min_x)/levels;
lowest_level=min_x+(step/2);
highest_level=max_x-(step/2);
level=lowest_level:step:highest_level;
level_number=ceil((x-min_x)/step);
quantized_value=level(level_number);
encoded = x/quantized_value*level_number;
z = dec2bin(encoded)
[rows, columns] = size(z);
for row = 1 : rows
for col = 1 : columns
if z(row, col) == '1'
num(row, col) = 1;
else
num(row, col) = -1;
end
end
end
num
You'll get:
z =
10×8 char array
'01011110'
'11001100'
'11101001'
'00000000'
'01001100'
'11111101'
'11010100'
'10110000'
'01111111'
'10000000'
num =
-1 1 -1 1 1 1 1 -1
1 1 -1 -1 1 1 -1 -1
1 1 1 -1 1 -1 -1 1
-1 -1 -1 -1 -1 -1 -1 -1
-1 1 -1 -1 1 1 -1 -1
1 1 1 1 1 1 -1 1
1 1 -1 1 -1 1 -1 -1
1 -1 1 1 -1 -1 -1 -1
-1 1 1 1 1 1 1 1
1 -1 -1 -1 -1 -1 -1 -1
Noman Abir
on 3 Jan 2021
Edited: Walter Roberson
on 3 Jan 2021
I am getting all binary values stays in a String. So, I want the output as a String also where all 0 will be replaced by -1. The string code you provided before is not working.
The code :
clc;
clear all;
close all;
bit=8;
levels=256;
Fs=8000;
t=0:1/Fs:(10-1)/Fs;
x = [-0.52, 1.2, 1.65, -1.99, -0.8, 1.96, 1.32, 0.76, 0, 0.01];
% stairs(t,x);
title('Sample values stairs version figure');
max_x = 2;
min_x = -2;
step=(max_x-min_x)/levels;
lowest_level=min_x+(step/2);
highest_level=max_x-(step/2);
level=lowest_level:step:highest_level;
level_number=ceil((x-min_x)/step);
quantized_value=level(level_number);
encoded = x/quantized_value*level_number;
z = dec2bin(encoded);
Binary values output :
01011110
11001100
11101001
00000000
01001100
11111101
11010100
10110000
01111111
10000000
My needed output :
-11-11111-1
11-1-111-1-1
111-11-1-11
-1-1-1-1-1-1-1-1
-11-1-111-1-1
111111-11
11-11-11-1-1
1-111-1-1-1-1
-11111111
1-1-1-1-1-1-1-1
Can you help me with this..?
Remember, all the binary values stays in a Sring.
Walter Roberson
on 3 Jan 2021
-11-11111-1
Exactly what is that? Is that a character vector ['-' '1' '1' '-' '1' '1' '1' '1' '1' '-' '1'] ? Is it the string array ["-1" "1" "-1" "1" "1" "1" "1" "-1"] ?
What is the overall output? Is it a character array? If so then is it acceptable if the short lines are padded with blanks?
Noman Abir
on 3 Jan 2021
I am working with a voice signal in CDMA method. I got this binary output (using "dec2bin" command) from the voice after doing many applications.
This is a digital voice binary output and I have to transmit it through the channel.
Image Analyst
on 3 Jan 2021
Edited: Image Analyst
on 3 Jan 2021
No. What we need to know is the class of the output. As you know, there are many different types of variables. We need to know which one you want because we keep getting conflicting messages from you. Here are some possibilities:
- int32
- uint8
- double
- character array
- string array
Now, pick just one and give us the number from the list above. They're all different so make sure you pick the right one.
Noman Abir
on 3 Jan 2021
Maybe, I got my solution. If I need any help about it, I will inform you guys. Thank you all.
Noman Abir
on 3 Jan 2021
Edited: Noman Abir
on 3 Jan 2021
Ok, I need some more help from you guys.
I can't post the questions with some unspecified error. Tha'ts why I am asking questions here.
I am looking for a way to multiply every row of a matrix to different values? Let assume we have:
A=[1 2 3;
4 5 6;
7 8 9]
B=[a b c]
I am looking for a way to have:
C=[1*a 2*b 3*c;
4*a 5*b 6*c;
7*a 8*b 9*c]
Thanks in advance for your comments.
Walter Roberson
on 3 Jan 2021
A=[1 2 3;
4 5 6;
7 8 9]
A = 3×3
1 2 3
4 5 6
7 8 9
●
B=[7 -2 5]
B = 1×3
7 -2 5
bsxfun(@times, A, B)
ans = 3×3
7 -4 15
28 -10 30
49 -16 45
●
Note: if you want symbolic values, syms a b c and you want C to be symbolic, then in your old release more work would have to be done. In releases since R2016b it is easier,
syms a b c
B = [a b c]
B =
A .* B
ans =
but in your old release, bsxfun does not support @times for symbolic values.
... But considering you are talking about communications, I do not think you need symbolic values, so I will not bother to code it up at the moment.
Image Analyst
on 3 Jan 2021
Another option:
a = 2;
b = 4
c = 10;
A=[1 2 3;
4 5 6;
7 8 9]
B=[a b c]
C = A .* repmat(B, [size(A, 2), 1])
Noman Abir
on 3 Jan 2021
Thanks. It's working.
Noman Abir
on 3 Jan 2021
I Have some values in a array like this.
A = -4
-4
-4
4
4
4
I want to convert all negative values to 0 and all positive values to 1.
How can I do that.??
Image Analyst
on 3 Jan 2021
A = double(A>0)
Noman Abir
on 3 Jan 2021
Can you pleae provide me full code?
Walter Roberson
on 3 Jan 2021
double(A>0)
is the full code, with the exception that it does not handle the possibility that A contains 0 exactly, which is something that you do not define the output for.
Noman Abir
on 3 Jan 2021
That code is not working in my version. Can you guys provide me an if-else process (using for loop) where 0 and all negative values will be replaced by "0" and all positive values will be replaced by "1".
Assume the sequence as :
A = [ -3 2 0 4 -2 1 3 -4 4]
Walter Roberson
on 3 Jan 2021
A = [ -3 2 0 4 -2 1 3 -4 4]
A = 1×9
-3 2 0 4 -2 1 3 -4 4
●
B = double(A>0)
B = 1×9
0 1 0 1 0 1 1 0 1
●
Is it possible that what you have is instead
AC = { '-3' '2' '0' '4' '-2' '1' '3' '-4' '4'}
AC = 1x9 cell array
{'-3'} {'2'} {'0'} {'4'} {'-2'} {'1'} {'3'} {'-4'} {'4'}
B = double(str2double(AC)>0)
B = 1×9
0 1 0 1 0 1 1 0 1
●
Walter Roberson
on 3 Jan 2021
AP = [ -3 2 0 4 -2 1 3 -4 4];
A = AP;
for K = 1 : numel(A)
if A(K) <= 0
A(K) = 0;
else
A(K) = 1;
end
end
disp(A)
0 1 0 1 0 1 1 0 1
A = AP;
A = double(A>0);
disp(A)
0 1 0 1 0 1 1 0 1
Noman Abir
on 4 Jan 2021
Thank you. I have got my solution.
Noman Abir
on 5 Jan 2021
I need some more help from you @Walter Roberson & @Image Analyst.
I have added 2 different MATLAB files where "receive_cdm.m" is my main code and "y_send.mat" is a file I am getting some values from it and loaded it in main code section.
If you look at the code then I have assigned 1 parameter c1. Forget about other parameters.
The following tasks are explained in the code.
I have done it with proper MATLAB commands and equations.
But, I am getting error when I calculate it manually and matching it with MATLAB calculations. (What I should get and what I am getting from MATLAB)
Can anyone check my code, please..?
It would be very helpful for me.
More Answers (1)
See Also
Categories
Find more on Matrix Indexing 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)