how to create a new variable based off an if else statement on one variable?
10 ビュー (過去 30 日間)
古いコメントを表示
I have created a table in MATLAB, one of the variables is called animals. Within that coloum are "dogs", "cats", "sheep". I want to create code that states if table.animals == "dogs" then create a new variable called numbers and assign the number 1 to the rows that have dogs stated in it. If table.animals == "cats" then in that same new variable called numbers assign the number 2. and similar with sheep expect the number 3. How would I go about create this type of code?
This is something similar to what I have tried but it does not work.
if tables.animals == "Dogs"
tables.numbers = 1
elseif tables.animals == "cats"
tables.numbers = 2
else tables.numbers = 3
end
my code ends up usually create that new coloum but places the number 3 for all the rows.
0 件のコメント
採用された回答
Voss
2022 年 12 月 1 日
t = table(["cats";"dogs";"cats";"sheep"],'VariableNames',"animals")
all_animals = ["dogs" "cats" "sheep"];
[~,idx] = ismember(t.animals,all_animals);
t.numbers = idx
4 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Data Type Identification についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!