Extract Data from Table by Data Values

조회 수: 1 (최근 30일)
Sam P
Sam P 2020년 5월 7일
편집: BhaTTa 2024년 7월 16일
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일
편집: BhaTTa 2024년 7월 16일
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