Get index from elements in a table

조회 수: 22 (최근 30일)
Fabio Pasquarella
Fabio Pasquarella 2016년 5월 16일
답변: Matt Cohen 2016년 5월 18일
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
Jan
Jan 2016년 5월 16일
Please post the code, which reproduces the problem. Then posting a matching answer is much easier. Thanks.
Fabio Pasquarella
Fabio Pasquarella 2016년 5월 16일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Matt Cohen
Matt Cohen 2016년 5월 18일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by