Need help filtering csv by values inside column

I have a large csv file(1034361x28). I want to look at column #1, and find rows containing the same column1 value and find the minimum value of column 2 for the new data set.
simple Ex.
if i have:
2 23
2 39
2 40
5 12
5 9
9 29
9 85
I need the code to find the minimum values for each subset of column 2(min of (23,39,40) and (12,9) and (29,85)).
How would i do so for a 1034361x28 file?
Thank You

 採用された回答

Star Strider
Star Strider 2019 年 7 月 8 日

2 投票

Try this:
A = [2 23
2 39
2 40
5 12
5 9
9 29
9 85];
[UA1,~,Ix] = unique(A(:,1));
Col2Max = accumarray(Ix, A(:,2), [], @min);
Out = [UA1, Col2Max];
producing:
Out =
2 23
5 9
9 29

6 件のコメント

Conor Nelson
Conor Nelson 2019 年 7 月 8 日
Worked like a charm. Thank You
Star Strider
Star Strider 2019 年 7 月 8 日
As always, my pleasure!
Conor Nelson
Conor Nelson 2019 年 7 月 11 日
Going off this, how can i look through the columns and create a separate array containing the rows with the same col1 values? Would i still use accumarray but without the @min?
Star Strider
Star Strider 2019 年 7 月 11 日
Would i still use accumarray but without the @min?
Yes. Only the function changes:
SameCol1 = accumarray(Ix, A(:,2), [], @(x){x});
SameCol1{1}
producing:
ans =
23
39
40
and similarly for the others.
It now creates a cell array of values correspoinding to each value of column 1.
Conor Nelson
Conor Nelson 2019 年 7 月 12 日
Sorry i think i worded my question wrong, I am looking to output the entire row containing the values. Ex:
ans =
2 23
2 39
2 40
Star Strider
Star Strider 2019 年 7 月 12 日
Try this:
SameCol1 = accumarray(Ix, (1:size(A,1)), [], @(x){A(x,:)});
SameCol1{1}
producing:
ans =
2 23
2 39
2 40
And so for the others.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by