이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
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 (한국어)