필터 지우기
필터 지우기

Keep only elements that appear multiple times

조회 수: 4 (최근 30일)
Ryan Magee
Ryan Magee 2013년 6월 12일
Hey everyone, been banging my head against this for awhile and can't come up with something efficient.
I have a large matrix, roughly 1000x1000, and I want to discard all elements (or replace with 0) that don't appear in the matrix at least three times. Additionally, I'm trying to allow for an error range so that, say, 10.1 and 9.9 (some arbitrary interval) will count as "10" (but this is secondary to the original problem).
I'm guessing that my main issue is that rewriting/editing a matrix is computationally expensive. The only solution I came up with involved numel in a loop, which is dreadfully slow.
Thanks for looking, advice is appreciated!

채택된 답변

Roger Stafford
Roger Stafford 2013년 6월 13일
Actually I didn't need to find the inverse of permutation p. The following is one step shorter:
[B,p] = sort(A(:));
t = [true;diff(B)~=0;true];
q = cumsum(t(1:end-1));
t = diff(find(t))<3;
A(p(t(q))) = 0;
  댓글 수: 2
Ryan Magee
Ryan Magee 2013년 6월 15일
I wound up going with this one as it was fastest. I'm pretty sure you permuted p,t,q from t,p,q in that last line though. Thanks for your help!
Roger Stafford
Roger Stafford 2013년 6월 15일
In the previous version I first took the inverse of p with the line
p(p) = 1:length(p);
and subsequently did this
A(t(q(p))) = 0;
It only occurred to me later that the inverse operation is not needed if we do the last step this way:
A(p(t(q))) = 0;
Without that inverse operation this last order p(t(q)) is essential. It wouldn't work otherwise.

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

추가 답변 (4개)

Roger Stafford
Roger Stafford 2013년 6월 12일
Here's a modification of Azzi's code that avoids the 'ismember' call.
[B,~,p] = unique(A(:));
t = histc(A(:),B)<3;
A(t(p)) = 0;

Roger Stafford
Roger Stafford 2013년 6월 13일
This version uses the 'sort' function instead of 'unique' and 'histc'. Consequently it might be faster.
[B,p] = sort(A(:));
p(p) = 1:length(p);
t = [true;diff(B)~=0;true];
q = cumsum(t);
t = diff(find(t))<3;
A(t(q(p))) = 0;

Andrei Bobrov
Andrei Bobrov 2013년 6월 13일
[a,b] = histc(A(:),unique(A));
A(a(b) < 3) = 0;

Azzi Abdelmalek
Azzi Abdelmalek 2013년 6월 12일
편집: Azzi Abdelmalek 2013년 6월 12일
A=[1 2 1 1;1 2 3 1;3 3 3 3;3 0 0 1];
B=unique(A(:));
A(ismember(A(:),B(histc(A(:),B)<3)))=0

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by