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 Kommentare

John D'Errico
John D'Errico am 2 Jan. 2021
Please. There was no reason to ask the same question twice.
Noman Abir
Noman Abir am 2 Jan. 2021
The network issue. That's why it submitted twice. sorry.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Image Analyst
Image Analyst am 2 Jan. 2021

1 Stimme

mask = yourSequence == 0; % Find all 0s
yourSequence(mask) = -1; % Convert 0s to -1s.

25 Kommentare

Noman Abir
Noman Abir am 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
Image Analyst am 2 Jan. 2021
Attach your binary sequence variable in a .mat file.
Noman Abir
Noman Abir am 2 Jan. 2021
Here, g is my binary sequence and I want to convert all the 0 values to -1.
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
Noman Abir am 2 Jan. 2021
It's showing error.
Noman Abir
Noman Abir am 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
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
Noman Abir am 3 Jan. 2021
Bearbeitet: Walter Roberson am 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
Walter Roberson am 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
Noman Abir am 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
Image Analyst am 3 Jan. 2021
Bearbeitet: Image Analyst am 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:
  1. int32
  2. uint8
  3. double
  4. character array
  5. 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
Noman Abir am 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
Noman Abir am 3 Jan. 2021
Bearbeitet: Noman Abir am 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.
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.
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
Noman Abir am 3 Jan. 2021
Thanks. It's working.
Noman Abir
Noman Abir am 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.??
A = double(A>0)
Noman Abir
Noman Abir am 3 Jan. 2021
Can you pleae provide me full code?
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
Noman Abir am 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]
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
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
Noman Abir am 4 Jan. 2021
Thank you. I have got my solution.
Noman Abir
Noman Abir am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Kategorien

Mehr zu Historical Contests finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by