フィルターのクリア

How to convert cell array to float array?

27 ビュー (過去 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 Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by