How to convert a table containing numbers and booleans to a double array?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
hanspeter
am 5 Jul. 2024
Kommentiert: Walter Roberson
am 5 Jul. 2024
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 Kommentare
the cyclist
am 5 Jul. 2024
Bearbeitet: the cyclist
am 5 Jul. 2024
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.
Akzeptierte Antwort
Voss
am 5 Jul. 2024
Bearbeitet: Voss
am 5 Jul. 2024
T = load('example.mat').data3
M = [strcmpi(T.var1,'TRUE') T.var2]
3 Kommentare
Voss
am 5 Jul. 2024
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"])
[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
Walter Roberson
am 5 Jul. 2024
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");
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Cell Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!