Extract Data from Table by Data Values

5 次查看(过去 30 天)
Sam P
Sam P 2020-5-7
编辑: BhaTTa 2024-7-16,3:17
Hi all,
I am trying to extract rows matching one of multiple values but I am not sure which functions to use. I have a 9,857,445 x 3 table. The headers are in the 3 columns, and all 3 columns A,B, C, contain numerical data.
I am trying to extract based on data for the first column A. For example, I want to extract the rows that have the following values for A; A= 1, A=6, A=4 or A =12. Based on all the rows that match any of the possible listed values for A, I would like to create a new table Final Data, that only lists rows that match the given values for A. I am using 2015a.
Thanks for your help.

回答(1 个)

BhaTTa
BhaTTa 2024-7-16,3:05
编辑:BhaTTa 2024-7-16,3:17
You can achieve this by using logical indexing below is the implemetation:
% Example to create a sample table, replace this with your actual data loading
dataTable = array2table(randi(20, 9857445, 3), 'VariableNames', {'A', 'B', 'C'});
valuesToMatch = [1, 6, 4, 12];
% Logical indexing to find rows where column A matches any of the specified values
rowsToExtract = ismember(dataTable.A, valuesToMatch);
% Create the new table with the extracted rows
finalData = dataTable(rowsToExtract, :);
% Example: Save to a new file
writetable(finalData, 'finalData.csv');

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by