How to convert cell array to float array?

34 vues (au cours des 30 derniers jours)
Nick
Nick le 6 Mai 2021
Modifié(e) : Stephen23 le 7 Mai 2021
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
...

Réponses (2)

Jan
Jan le 6 Mai 2021
Modifié(e) : Jan le 6 Mai 2021
% 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 commentaires
Nick
Nick le 6 Mai 2021
I did that but I'm getting an error :
Error using bin2dec (line 36)
Input must be a character vector.
Jan
Jan le 6 Mai 2021
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...

Connectez-vous pour commenter.


Stephen23
Stephen23 le 6 Mai 2021
Modifié(e) : Stephen23 le 7 Mai 2021
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

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by