フィルターのクリア

How so subset observations in a matrix?

1 回表示 (過去 30 日間)
Prerna Mishra
Prerna Mishra 2022 年 7 月 25 日
回答済み: KSSV 2022 年 7 月 25 日
I have a matrix that has the follwing columns:
[GVKEY FYEAR SALE ASSET]
FYEAR ranges from 2005-2019 and there are many GVKEYs which are basically identifiers. I want to keep only those GVKEYS(observations) for which I have the entire 2005-2019 observations, as some may have only sub ranges like 2007-2009.
How do I do this?

採用された回答

Githin George
Githin George 2022 年 7 月 25 日
編集済み: Githin George 2022 年 7 月 25 日
Hi Prerna,
My understanding is that you want to filter out the rows of a matrix for those GVKEYs having observations from 2005-2019. I would like to make the assumption that the year range is continuous. You could try out the following piece of code.
keys = unique(data(:,1))
for i=1:numel(keys)
keyData = data(data==keys(i),:)
% from here you can compare with keyData length or compare the year
% array to the 2nd column and based on that modify values in the actual
% data array.
col2 = sort(keyData(:,2))
flag = isequal(col2,2005:2019)
if(flag == 0)
data(data==keys(i),:) = []
end
end
I Hope this helps.
  7 件のコメント
Prerna Mishra
Prerna Mishra 2022 年 7 月 25 日
Thanks so much!
Prerna Mishra
Prerna Mishra 2022 年 7 月 25 日
A follow up, if i want to filter rows where column 7 begins with 31, how would I do that?

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

その他の回答 (1 件)

KSSV
KSSV 2022 年 7 月 25 日
idx = FYEAR >= 2007 & FYEAR <= 2009 ;
iwant_FYEAR = FYEAR(idx) ;
iwant_GVKEYS = GVKEYS(idx) ;

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by