I have a matrix:
a b c
1 201 1
2 202 1
3 203 1
4 204 1
5 205 1
6 206 1
7 207 2
8 208 2
9 209 2
10 210 2
11 222 3
12 232 3
I would like to remove rows 7-12 which have a value in the last column (c) repeated less than 5 times. Any help is appreciated.

 채택된 답변

Sean de Wolski
Sean de Wolski 2017년 8월 31일
편집: Sean de Wolski 2017년 8월 31일
m = [1 201 1
2 202 1
3 203 1
4 204 1
5 205 1
6 206 1
7 207 2
8 208 2
9 209 2
10 210 2
11 222 3
12 232 3];
% Unique elements and locations in third column
[uv, ~, id] = unique(m(:,3));
% How many of each?
n = histcounts(id);
% Keep ones with more than 5.
m2 = m(ismember(m(:,3), uv(n>=5)),:)

댓글 수: 2

Thank you very much for your help, Sean. It is definitely what I'm looking for.
Hello Sean, your code works perfectly fine on a small testfile I use (190 rows).
But as soon as I add some more rows, the
n = histcounts(id);
part starts giving weird values. I cannot find the reason for that change.
Attached you find the two files:
working.txt code runs through without any trouble
not working.txt "n = histcounts(id);" gives strange output and the m2 stays empty.
Any suggestions on how the code could be adjusted to work on a bigger file?
since my file looks a little different (I am searching in the first column) here is the code I use:
if true
load working.txt
a = working(:,:);
[uv, ~, id] = unique(a(:,1));
n = histcounts(id);
m2 = a(ismember(a (:,1), uv(n==4)),:);
end

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

José-Luis
José-Luis 2017년 8월 31일
편집: José-Luis 2017년 8월 31일
idx = unique(m(:,3));
count = accumarray(c.',(1:numel(c)).',[],@numel);
to_keep = ~ismember(m(:,3),idx(count < 5));
result = m(to_keep,:)

댓글 수: 5

Dinh-Vinh Vo
Dinh-Vinh Vo 2017년 8월 31일
편집: Dinh-Vinh Vo 2017년 8월 31일
Hi Jose-Luis, Thanks for your answer. Could you please explain what c is in the second row?
My bad.
c = m(:,3);
idx = unique(c);
count = accumarray(c, (1:numel(c)).',[],@numel);
to_keep = ~ismember(c,idx(count < 5));
result = m(to_keep,:)
If I change the matrix to: m = [1 201 1 2 202 1 3 203 1 4 204 1 5 205 1 6 206 1 7 207 2 8 208 2 9 209 2 10 210 2 11 222 5 12 232 9];
It doesn't work any longer :(.
No, the solution is not robust. You'd need to adjust the accumarray function.
Thanks, I did it.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2017년 8월 31일

편집:

2018년 3월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by