フィルターのクリア

extracting rows from large table

8 ビュー (過去 30 日間)
Mark Bodner
Mark Bodner 2022 年 9 月 30 日
コメント済み: Walter Roberson 2022 年 10 月 11 日
I have a large table (23000000x11) and want to extract rows from the table based on values in the 5th column.
for example the data looks like:
66172 8:17 1 2 1 1 1 1 1 6 8
66842 7:17 1 3 0 1 1 1 1 1 7
.
.
.
and I want to create a smaller table with all the rows whose value of the fifth column are 0. This is of course easy to do with for loops and if statements, but takes enormous amounts of time to run. Is there a way using maybe ismember, intersect, or some other means to carry this out. I'm sure there must be. Any suggestions are appreciated.

採用された回答

Walter Roberson
Walter Roberson 2022 年 9 月 30 日
mask = YourTable{:, 5} == 0;
subset0 = YourTable(mask, :);
  3 件のコメント
Siddharth Bhutiya
Siddharth Bhutiya 2022 年 10 月 10 日
For the first line of code dot would be much faster than brace.
mask = YourTable.varName == 0; % if you know the name of your fifth variable
mask = YourTable.(5) == 0; % if you dont know the name.
Walter Roberson
Walter Roberson 2022 年 10 月 11 日
I was curious about the timing.
In my tests, the first 5 or so {:,5} accesses were reliably much slower than the others, so I skip those in the plot.
I must admit that I do not understand why {:,5} would be 5 or 6 times slower than .(5)
format long g
YourTable = array2table(randi(9, 5000, 7), 'VariableNames', {'a', 'b', 'c', 'd', 'e', 'f', 'g'});
N = 50;
skip = 7;
time_brace = zeros(N,1);
time_named = zeros(N,1);
time_dotnum = zeros(N,1);
for K = 1 : N
tic; YourTable.(5); t=toc(); time_dotnum(K) = t;
tic; YourTable{:,5}; t=toc(); time_brace(K) = t;
tic; YourTable.e; t=toc(); time_named(K) = t;
end
subset = [time_brace(skip+1:end), time_named(skip+1:end), time_dotnum(skip+1:end)];
plot(subset);
legend({'\{:,5\}', '.e', '.(5)'})
mean(subset)
ans = 1×3
1.0e+00 * 7.27209302325581e-05 1.4046511627907e-05 1.01627906976744e-05

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by