Producing strings in empty column based on strings on another column
1 回表示 (過去 30 日間)
古いコメントを表示
I have a spreadsheet with a blank column (57x1) that I want filled out based on the strings on another column (57x1).
Basically
If column 5 contains brad, the corresponding row in column 7 should have brad
If column 5 contains fred, the corresponding row in column 7 should have fred
If column 5 is a contains mike , angie, or alex, the corresponding row in column 7 should have oscar.
If column 5 contains any other name (jack, joe, etc), the corresponding row in column 7 should have roger
Column 5
mike
mike
brad
brad
angie
fred
fred
alex
jack
joe
matt
brad
Column 7 should look like this
oscar
oscar
brad
brad
oscar
fred
fred
oscar
roger
roger
roger
brad
How do I do this ?
1 件のコメント
Image Analyst
2020 年 6 月 18 日
Try strcmpi() or contains(). Make it easy for people to help you. Give us your table in a .mat file, or give code to construct it. It will make it easier for people to try things and help you.
採用された回答
Mara
2020 年 6 月 18 日
編集済み: Mara
2020 年 6 月 18 日
array(:,5) = ["mike" "mike" "brad" "angie"]
sz = size(array, 1);
for i = 1:sz
name = array(i, 5);
switch name
case "mike"
array(i,7) = "oscar"
case "brad"
array(i,7) = "brad"
case "angie"
array(i,7) = "oscar"
otherwise
array(i,7) = "roger"
end
end
Does this help you?
6 件のコメント
Mara
2020 年 6 月 24 日
編集済み: Mara
2020 年 6 月 24 日
Yes you can probably find a way to do it with an if statement. Another way would be by using two logical vectors. Note that in the previous example "allmikes" is a logical vector of 0(false) and 1(true). You can create logical vectors for both columns and put an input in column 5 where both is true.
Note that strcmpi() will not give you any true result for column four, since it only compares the whole strings. But you can use contains().
ar((strcmpi(ar(:,1), "fast") & contains(ar(:,2), "boat")), 5) = "boat"
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Tables についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!