How do you remove a value from a table based on its value
20 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Daniel Gaggini
am 7 Mai 2023
Kommentiert: Daniel Gaggini
am 8 Mai 2023
I am trying to remove the columns from the tollowing Combined table if they have a Value of 1.
I am a trying to find a method that deletes the colunn based on its value rather than deleting the column based on its heading, as it value may change to a 1 or 0 depending on the data set.
This is what I currently have but I am recieving the following error. If anyone has any advice that would be great!
Value = ([0,0,1,1,0])
Heading = (["Radius", "Speed", "Type", "Location", "ID"])
Combined = array2table([Value; Heading])
Combined(Combined(1,:)>0,:) = []
2 Kommentare
VBBV
am 8 Mai 2023
% first approach & simpler
Value = ([0,0,1,1,0]);
idx = Value > 0;
Heading = (["Radius", "Speed", "Type", "Location", "ID"]);
Combined = array2table([Value; Heading]);
Combined(:,idx) = []
% second approach using additional functions
Value = ([0,0,1,1,0]);
Heading = (["Radius", "Speed", "Type", "Location", "ID"]);
Combined = array2table([Value; Heading]);
idx = str2double(table2array(Combined(1,:))) > 0
Combined(:,idx) = []
Akzeptierte Antwort
the cyclist
am 7 Mai 2023
Bearbeitet: the cyclist
am 7 Mai 2023
One can do what you ask as follows:
Value = ([0,0,1,1,0]);
Heading = (["Radius", "Speed", "Type", "Location", "ID"]);
Combined = array2table([Value; Heading]);
Combined(:,Combined{1,:} == "1") = []
In your simple example, MATLAB had to convert the numeric values 0 and 1 to the strings "0" and "1", to store them in the same column with other strings.
2 Kommentare
the cyclist
am 7 Mai 2023
Also, I can't shake the feeling that you are storing your data in a table in a very unconventional way, and therefore will experience many of these awkward operations. I suggest learning about the tidy format.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical 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!