Filling out an empty column in an existing table based on conditions matching other columns

7 次查看(过去 30 天)
I have a table. If column 3 is "fast" AND column 4 contains any of these characters: boat, car, or airplane. then column 5 become those strings on column 4. If it meets just the "fast" in column 3, then column 5 becomes "fast". Otherwise it's none. Do I need an if else statement? Or how would I do this?
column 3
fast
fast
slow
medium
fast
column 4
mike boat day
pax airplane oscar
ship
car
train
column 5
boat
airplane
none
none
fast
  1 个评论
Sole
Sole 2020-6-23
I tried this and it didn't work. It still gives me "fast" even though the allnames contains a 1 as the logic for detecting "boat" in the columns
t1 = table();
str = table2cell(lut(:,3));
allnames = contains(str,"boat");
if allnames == 1
t1.secondnames(allnames,:) = "boat";
else
t1.secondnames(allnames,:) = "fast";
end

请先登录,再进行评论。

采纳的回答

Mohammad Sami
Mohammad Sami 2020-6-24
% t1 = yourtable
logic1 = startsWith(t1.column3varname,'fast') & startsWith(t1.column4varname,{'boat' 'car' 'airplane'});
logic2 = startsWith(t1.column3varname,'fast') & ~startsWith(t1.column4varname,{'boat' 'car' 'airplane'});
t1.column5varname(logic1) = t1.column4varname(logic1);
t1.column5varname(logic2) = t1.column3varname(logic2);
t1.column5varname(~logic1 & ~logic2) = {'none'};

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by