フィルターのクリア

How to convert a table containing numbers and booleans to a double array?

46 ビュー (過去 30 日間)
hanspeter
hanspeter 2024 年 7 月 5 日 20:47
コメント済み: Walter Roberson 2024 年 7 月 5 日 23:19
using table2array I get a string array, but I need an array of numbers where the booleans become 0 or 1 respectivly. double(table2array(arr)) results in NaN for every boolean value
  2 件のコメント
the cyclist
the cyclist 2024 年 7 月 5 日 21:29
編集済み: the cyclist 2024 年 7 月 5 日 21:30
It would be easier for us to suggest a solution if you uploaded a representative sample of your data. You can use the paper clip icon in the INSERT section of the toolbar.
hanspeter
hanspeter 2024 年 7 月 5 日 22:14
whoops, I just noticed that the "booleans" weren't actual booleans, but string that were either "TRUE" or "FALSE". I was sure I checked that. With acutal booleans a simple table2array works just fine.
Anyway I attached an example table as the issue still persists. How do I convert this table to an array of doubles?

サインインしてコメントする。

回答 (1 件)

Voss
Voss 2024 年 7 月 5 日 22:40
編集済み: Voss 2024 年 7 月 5 日 23:00
T = load('example.mat').data3
T = 1x2 timetable
time var1 var2 _____ _______ ____ 0 sec "FALSE" 2.52
M = [strcmpi(T.var1,'TRUE') T.var2]
M = 1x2
0 2.5200
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  3 件のコメント
Voss
Voss 2024 年 7 月 5 日 23:10
Here's one way to do that:
T = table(["FALSE";"TRUE";"FALSE";"FALSE"],[1.1;2.2;3.3;4.4],["TRUE";"TRUE";"FALSE";"TRUE"])
T = 4x3 table
Var1 Var2 Var3 _______ ____ _______ "FALSE" 1.1 "TRUE" "TRUE" 2.2 "TRUE" "FALSE" 3.3 "FALSE" "FALSE" 4.4 "TRUE"
[m,n] = size(T);
M = NaN(m,n);
for ii = 1:n
if isstring(T.(ii))
M(:,ii) = strcmpi(T.(ii),'TRUE');
else
M(:,ii) = T.(ii);
end
end
M
M = 4x3
0 1.1000 1.0000 1.0000 2.2000 1.0000 0 3.3000 0 0 4.4000 1.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Walter Roberson
Walter Roberson 2024 年 7 月 5 日 23:19
One approach would be to use
mask = varfunc(@isnumeric, T, OutputFormat = "uniform");
Output = zeros(height(T), width(T));
Output(:,mask) = T{:,mask};
Output(:,~mask) = varfunc(@(V)strcmpi(V, "TRUE"), InputVariables = ~mask, OutputFormat = "uniform");

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by