Subset a table based on a column and another vector

4 ビュー (過去 30 日間)
Steven Vits
Steven Vits 2021 年 5 月 6 日
回答済み: Tejas 2025 年 7 月 10 日
So I have a table of values and one of the columns is patient_number. I have another vector of patient numbers I want to get information about from the table.
How do I subset the table based on the second vector that I have? I want the resulting subset to be in the same order of the subseting vector.
Thanks!

回答 (1 件)

Tejas
Tejas 2025 年 7 月 10 日
Hello Steven,
To acheive the desired workflow, use the "ismember" function to find the indexes of patients from the table, that match with the patients in the vector. For more information on "ismember" function, refer to this documentation: https://www.mathworks.com/help/matlab/ref/double.ismember.html .
Below is an example showcasing how the "ismember" function can be used to acheive the desired workflow:
  • Create a sample table and the second vector.
T = table([101; 102; 103; 104], {'Alice';'Bob';'Charlie';'Dana'}, ...
'VariableNames', {'patient_number', 'name'});
wantedPatients = [101 103];
  • Use the "ismember" function to find the matching indexes and use them to retrieve the subset of table.
[found, idx] = ismember(wantedPatients, T.patient_number);
subset_T = T(idx(found), :);

カテゴリ

Help Center および File ExchangeTables についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by