Binary value conversion.
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Image Analyst
2021-1-2
mask = yourSequence == 0; % Find all 0s
yourSequence(mask) = -1; % Convert 0s to -1s.
25 个评论
Noman Abir
2021-1-2
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
2021-1-2
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
2021-1-2
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
2021-1-2
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
2021-1-3
编辑:Walter Roberson
2021-1-3
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
2021-1-3
-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
2021-1-3
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
2021-1-3
编辑:Image Analyst
2021-1-3
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
2021-1-3
Maybe, I got my solution. If I need any help about it, I will inform you guys. Thank you all.
Noman Abir
2021-1-3
编辑:Noman Abir
2021-1-3
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
2021-1-3
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
2021-1-3
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
2021-1-3
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.??
Walter Roberson
2021-1-3
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
2021-1-3
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
2021-1-3
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
2021-1-3
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
2021-1-5
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.
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)