Remove rows based on length of column value

2 ビュー (過去 30 日間)
Jaehwi Bong
Jaehwi Bong 2019 年 6 月 3 日
コメント済み: Rik 2019 年 6 月 4 日
I have a matrix , 1531*3 double.
At a first column, numbers represent same particles from 1 to 14. I have 14 particles and each of them has different time points(second column) with intensity value(third column).
I'd like to take only particles which the number of time points is over 2, and make a new matrix. In this example, I need to remove rows of 14th particle and make the new matrix which doesn't have 14th particle, because it has only 2 time points. Should I use 'loop'?
for j = 1:14
if length(find(A(:,1)==j)) > 2
end
end

採用された回答

Rik
Rik 2019 年 6 月 3 日
Instead of implicit expansion (which can take a lot of memory for bigger arrays), you can use histogram counts:
S=load('practice.mat');
A=S.A;
c=histcounts(A(:,1),numel(unique(A(:,1))));
remove_ID=find(c<=2);
L=ismember(A(:,1),remove_ID);
B=A(~L,:);%keep only IDs with >2 time points
  2 件のコメント
Jaehwi Bong
Jaehwi Bong 2019 年 6 月 4 日
編集済み: Jaehwi Bong 2019 年 6 月 4 日
Thank you very much, I haven't thought about using the histogram! It seems to work well.
Quick question: If I want to check whether it works well, what can I do for that?
number=unique(B(:,1));
tn=length(unique(B(:,1)));
for j=1:tn
B([length(find(B(:,1)==j))>2],:)=[];
end
Rik
Rik 2019 年 6 月 4 日
I have no clue what you aim to do with that code. It looks like you want to confirm my code works as expected by comparing it to a for-loop. If that is what you want your code in the question is actually closer:
B=A;
for j = 1:14
match=find(B(:,1)==j);
if length(match) <= 2
B(match,:)=[];
end
end

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

その他の回答 (1 件)

Joel Handy
Joel Handy 2019 年 6 月 3 日
I think this code will do what you want. It will make a vector with the same number of rows as A, with each element being the number of times that ID shows up in ID column, and then delete all of the rows where the ID count is less than or equal to 2. It takes advantage of implicit expansion to do this.
numTimes = sum(A(:,1)==A(:,1)');
A(numTimes<=2,:) = [];
  1 件のコメント
Jaehwi Bong
Jaehwi Bong 2019 年 6 月 4 日
Thank you very much. It was helpful :)

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

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by