Get index from elements in a table

36 views (last 30 days)
I'm new to matlab, coming from data analysis in python/Panda. I have some problems working on tables of different data. Let's say I've a table T with some students data stored: one column with names and others with votes (from 0 to 10) for each exam column. I need to find the indexes of students who have votes>=5.
Thank you
  2 Comments
Jan
Jan on 16 May 2016
Please post the code, which reproduces the problem. Then posting a matching answer is much easier. Thanks.
Fabio Pasquarella
Fabio Pasquarella on 16 May 2016
I'm sorry, I don't have any code, it was just an example. I need to know how to extract indexes from a table given a logical statement on some columns. Sorry for my english

Sign in to comment.

Accepted Answer

Matt Cohen
Matt Cohen on 18 May 2016
Hi Fabio,
I understand that you are interested in learning more about how to index into tables using logical expressions.
The MATLAB documentation section titled Access Data in a Table contains an example that shows how to index into a table using a logical expression. I have included some of the example code below, as well as an additional example to the indexing process.
load patients
patients = table(Age,Gender,Height,Weight,Smoker,...
'RowNames',LastName);
rows = patients.Age<30;
vars = {'Gender','Height','Weight'};
T1 = patients(rows,vars);
T2 = patients(patients.Age>30, {'Gender','Height'});
The "patients" data provided by MATLAB is used here. In this example, a new table, 'T1', is created that contains the gender, height, and weight of the patients under the age of 30. The only rows selected are the ones where the value in the variable, 'Age', is less than 30. The dot notation is used to extract data from the table variable, and a logical expression is used to define the subset of rows based on that extracted data. The variable 'rows' is a 100-by-1 logical array containing logical true (1) for rows where the value in the variable, 'Age', is less than 30. Finally, parentheses are used to perform the indexing and to return a table containing the desired subset of the data. A second table, 'T2', is also created and contains the gender and weight data of the subset of patients whose age is over 30.
I hope this information proves to be helpful.
Matt

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!