How to convert cell array to float array?

조회 수: 38 (최근 30일)
Nick
Nick 2021년 5월 6일
편집: Stephen23 2021년 5월 7일
Hello! I have a cell array (1000x11) of binary digits like that:
0 1 0 0 0 0 1 0 0 0 0
0 1 0 1 1 1 0 1 1 1 0
1 1 1 0 0 1 0 1 1 1 0
0 0 1 1 1 1 1 0 1 1 0
0 0 0 1 0 0 0 0 1 1 1
0 0 0 0 0 1 0 0 0 1 1
...
Each 11 digit row represents a float number, where the last 10 digits are the numbers after the dot. How can I convert that cell array to an array (1000x1) of decimal floats for example :
0.528
0.750
1.814
0.502
0.135
0.035
...

답변 (2개)

Jan
Jan 2021년 5월 6일
편집: Jan 2021년 5월 6일
% If the input is a cell containing the chars '0' and '1':
B = cell2mat(YourCell);
Value = B(:, 1) - '0' + bin2dec(B(:, 2:11)) / 1000;
[EDITED] and if the values are the doubles 0 and 1:
B = cell2mat(YourCell);
Value = B(:, 1) + (B(:, 2:11) * 2.^(9:-1:0).') / 1000;
By the way, this would be more efficient for the first case also:
B = cell2mat(YourCell) - '0';
Value = B(:, 1) + (B(:, 2:11) * 2.^(9:-1:0).') / 1000;
  댓글 수: 2
Nick
Nick 2021년 5월 6일
I did that but I'm getting an error :
Error using bin2dec (line 36)
Input must be a character vector.
Jan
Jan 2021년 5월 6일
Unfortunately you did not provide some input data, which I could download or use by copy&paste. Therefore my answer contained some guessing about the type of your inputs.
I'm adding a version for numerical values in the input cell, wait some minutes...

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


Stephen23
Stephen23 2021년 5월 6일
편집: Stephen23 2021년 5월 7일
M = [... as a numeric matrix (e.g. CELL2MAT or STR2DOUBLE)
0 1 0 0 0 0 1 0 0 0 0
0 1 0 1 1 1 0 1 1 1 0
1 1 1 0 0 1 0 1 1 1 0
0 0 1 1 1 1 1 0 1 1 0
0 0 0 1 0 0 0 0 1 1 1
0 0 0 0 0 1 0 0 0 1 1];
V = pow2(9:-1:0)/1000;
D = M(:,1)+M(:,2:end)*V(:)
D = 6×1
0.5280 0.7500 1.8140 0.5020 0.1350 0.0350
Or, depending on how those binary digits are specified:
V = pow2(0:-1:-10);
D = M*V(:)
D = 6×1
0.5156 0.7324 1.7949 0.4902 0.1318 0.0342

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by