Binary numbers into array

조회 수: 6 (최근 30일)
Rosario Frontino
Rosario Frontino 2022년 3월 27일
댓글: Voss 2022년 3월 27일
I have a string n of N numbers (like '21325231214523234322' ) and their respective Huffman code ( {'10' } {'01' } {'00' } {'110' } {'111' } )
I would like to create an array with all the stream bits (01 10 00 ...)
stream = zeros(1,N);
for i=1:N
value = str2num(n(i));
stream(i) = str2num(ret{3,value});
end
where ret is a cell array with values, probabilities and then the Huffman codes.
This is not working because the codes starting with 0 will be converted to numbers and the 0s are lost (01 -> 0 , 00 -> 0).
Moreover,
stream(i) = ret{3,value};
is also not working: Unable to perform assignment because the left and right sides have a different number of elements.
How can I create my array?

채택된 답변

Rosario Frontino
Rosario Frontino 2022년 3월 27일
편집: Rosario Frontino 2022년 3월 27일
Solved.
If anyone's curious:
bitStream = {};
for i=1:N
value = str2num(n(i));
bitStream(i) = ret(3,value);
end
  댓글 수: 1
Voss
Voss 2022년 3월 27일
This solution relies on the numbers being integers starting with 1, so that you can use them as indices.
What happens if your numbers are like this? (Hint: an error occurs)
n = '76870786769078789877';
ret = { ...
'6' '7' '8' '9' '0'; ...
0.5 0.3 0.1 0.05 0.05; ...
'10' '01' '00' '110' '111'}
ret = 3×5 cell array
{'6' } {'7' } {'8' } {'9' } {'0' } {[0.5000]} {[0.3000]} {[0.1000]} {[0.0500]} {[0.0500]} {'10' } {'01' } {'00' } {'110' } {'111' }
try
bitStream = {};
N = numel(n);
for i=1:N
value = str2num(n(i));
bitStream(i) = ret(3,value);
end
catch ME
disp(ME.message);
end % error:
Index in position 2 exceeds array bounds. Index must not exceed 5.
No error occurs in this case with my answer:
[~,idx] = ismember(n,[ret{1,:}]);
bitStream = ret(3,idx)
bitStream = 1×20 cell array
{'01'} {'10'} {'00'} {'01'} {'111'} {'01'} {'00'} {'10'} {'01'} {'10'} {'110'} {'111'} {'01'} {'00'} {'01'} {'00'} {'110'} {'00'} {'01'} {'01'}

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Voss
Voss 2022년 3월 27일
Depending on what exactly you're after, one of these two very similar ways may work:
n = '21325231214523234322';
ret = { ...
'1' '2' '3' '4' '5'; ...
0.5 0.3 0.1 0.05 0.05; ...
'10' '01' '00' '110' '111'}
ret = 3×5 cell array
{'1' } {'2' } {'3' } {'4' } {'5' } {[0.5000]} {[0.3000]} {[0.1000]} {[0.0500]} {[0.0500]} {'10' } {'01' } {'00' } {'110' } {'111' }
[~,idx] = ismember(n,[ret{1,:}]);
% 1) stream is a character array of '0's and '1's:
stream = [ret{3,idx}]
stream = '01100001111010010011011011101000100110000101'
% or, 2) stream is a cell array of codewords:
stream = ret(3,idx)
stream = 1×20 cell array
{'01'} {'10'} {'00'} {'01'} {'111'} {'01'} {'00'} {'10'} {'01'} {'10'} {'110'} {'111'} {'01'} {'00'} {'01'} {'00'} {'110'} {'00'} {'01'} {'01'}

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by