Obtain all table elements from a column which correspond to certain string in another column

1 次查看(过去 30 天)
Hello,
I have a table with two columns. The first is a column of strings & the second numbers. I want to obtain all the elements in column 2 that correspond to the string 'Bonus' in column 1.
How can I do this?
Thank you

采纳的回答

Voss
Voss 2023-3-5
% a table
str = {'Onus';'Bonus';'Loan us';'On us';'Bonus';'Money';'Bonus'};
num = [1;2;3;2;4;3;1];
t = table(str,num)
t = 7×2 table
str num ___________ ___ {'Onus' } 1 {'Bonus' } 2 {'Loan us'} 3 {'On us' } 2 {'Bonus' } 4 {'Money' } 3 {'Bonus' } 1
% get the numbers corresponding to 'Bonus'
bonus_nums = t{strcmp(t{:,1},'Bonus'),2}
bonus_nums = 3×1
2 4 1

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by