現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Filtering Columns of Array by Number of Row Contents
1 回表示 (過去 30 日間)
古いコメントを表示
I have arrays that look like this:
A =
1 NaN NaN
2 3 4
2 5 NaN
I want to remove columns that contain rows with less than n non-NaN entries. In this case, with n=2, the first column of A would be removed, since the first row contains only 1 non-NaN value. Is there a compact way to do this? I hope this explanation makes sense!
採用された回答
Ameer Hamza
2020 年 3 月 27 日
編集済み: Ameer Hamza
2020 年 3 月 27 日
A = [1 NaN NaN
2 3 4
2 5 NaN];
n = 2;
A(sum(~isnan(A), 2) < n, :) = [];
Result
A =
2 3 4
2 5 NaN
16 件のコメント
Justin Delano
2020 年 3 月 27 日
I'm sorry, I just realized this wasn't the result I'm looking for. I'm looking for the result:
A = [NaN NaN
3 4
5 NaN]
Is there a solution for this problem?
Ameer Hamza
2020 年 3 月 30 日
Are you looking for something like this
A = [1 NaN NaN
2 3 4
2 5 NaN];
n = 2;
A(:, sum(~isnan(A), 2) < n) = [];
Justin Delano
2020 年 3 月 30 日
Hello, I can give another example.
Input =
1 2 NaN
10 NaN 5
1 3 5
NaN NaN 6
Output =
1 2
10 NaN
1 3
NaN NaN
Column 3 was removed due to it having a value in row 4, which contains more than n=2 NaNs.
Ameer Hamza
2020 年 3 月 30 日
try this:
A = [1 2 NaN
10 NaN 5
1 3 5
NaN NaN 6];
idx = sum(isnan(A),2) >= 2;
idx2 = find(~isnan(A(idx, :)));
A(:,idx2) = [];
Justin Delano
2020 年 3 月 30 日
Thank you, that seems closer. I didn't expect this to be such a tough problem. If need be I'll do this with loops instead of vectorized functions. That attempt works on that test case, but throws an error on this one:
A =
1 NaN NaN
10 NaN 5
1 3 5
NaN NaN 6
Ameer Hamza
2020 年 3 月 30 日
I realized I overlooked a things in my last code. Does following code work
A = [1 NaN NaN
10 NaN 5
1 3 5
NaN NaN 6];
idx = sum(isnan(A),2) >= 2;
[~, idx2] = find(~isnan(A(idx, :)));
A(:,idx2) = [];
Justin Delano
2020 年 3 月 30 日
編集済み: Justin Delano
2020 年 3 月 30 日
Thank you, that is better. Unfortunately, for my data, there are a lot more NaNs. I have attached two example matrices. When I use your code, the first matrix is turned completely blank, which I don't think should be the case.
Ameer Hamza
2020 年 3 月 30 日
I am still not entirely sure what is the correct rule to delete columns. Can you write a simple for loop version for a small matrix. I will try to see if it can be vactorized.
Justin Delano
2020 年 3 月 30 日
編集済み: Justin Delano
2020 年 3 月 30 日
Here is a rough loop version:
%scan each column of full matrix
for column = matrix_columns
%scan each row of each column
for entry = column_rows
%assume entry is from row = entry_row in full matrix
if ~isnan(entry)
if sum(~isnan(entry_row)) < 10
%delete this column from full matrix
continue
end
end
end
end
Ameer Hamza
2020 年 4 月 1 日
Justin, there some to be some issue in this loop version too. For example If I apply it to your previous example, it still does not give the result you want
x = [1 2 NaN
10 NaN 5
1 3 5
NaN NaN 6];
col_delete = [];
%scan each column of full matrix
for col = 1:size(x,2)
%scan each row of each column
for row = 1:size(x,1)
entry = x(row, col);
%assume entry is from row = entry_row in full matrix
if ~isnan(entry)
entry_row = x(row, :);
if sum(~isnan(entry_row)) < 10
%delete this column from full matrix
col_delete = [col_delete col];
end
end
end
end
x(:,unique(col_delete)) = [];
Final value of x should be
1 2
10 NaN
1 3
NaN NaN
But your loop version make it an empty matrix.
Justin Delano
2020 年 4 月 1 日
I apologize, you are correct. I set the sum in the final if statement to be less than 10, but in reality it should be less than n, which we have set to 2.
Ameer Hamza
2020 年 4 月 1 日
Ok. Now it is clear. Please try
x = fate1nums; % variable from file you shared
n = 10;
[r, c] = find(~isnan(x));
x(:, c(sum(~isnan(x(r,:)), 2) < n)) = [];
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で NaNs についてさらに検索
タグ
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)