現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Binary value conversion.
2 ビュー (過去 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).
Noman Abir
2021 年 1 月 2 日
Here, g is my binary sequence and I want to convert all the 0 values to -1.
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 = ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/477403/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/477403/image.png)
A .* B
ans =
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/477408/image.png)
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!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)