フィルターのクリア

conditionally deleting matrix rows

9 ビュー (過去 30 日間)
Abdelwahab Fawzy
Abdelwahab Fawzy 2016 年 7 月 13 日
コメント済み: Image Analyst 2022 年 2 月 19 日
Hi all,
I wish to delete matrix rows according to a condition imposed over its columns. let assume the matrix is of 10x2 (rows x columns) size.
A= [16 45; 79 8; 31 23; 53 91; 17 15; 60 83; 26 54; 65 100; 69 8; 75 44]
I want to delete the row if either A(i,1) or A(i,2) is larger than 50
A= [16 45; 31 23; 17 15]

採用された回答

Image Analyst
Image Analyst 2016 年 7 月 13 日
Try this:
A= [16 45; 79 8; 31 23; 53 91; 17 15; 60 83; 26 54; 65 100; 69 8; 75 44]
rowsToDelete = any(A>50, 2)
A(rowsToDelete,:) = []
  4 件のコメント
Romain Hautier
Romain Hautier 2022 年 2 月 19 日
Hello,
I know it has been a long time since you answered but I have an additional question. Is it possible to delete rows and colums satisfying a condition all at once? Say I have a matrix A = [1 2 3; 3 0 0; 1 2 0], would I be able to delete all rows and columns containing a 0 all at once to be left with A = [1]?
Thank you in advance.
Image Analyst
Image Analyst 2022 年 2 月 19 日
A = [1 2 3; 3 0 0; 1 2 0]
A = 3×3
1 2 3 3 0 0 1 2 0
rowsToDelete = any(A == 0, 2)
rowsToDelete = 3×1 logical array
0 1 1
columnsToDelete = any(A == 0, 1)
columnsToDelete = 1×3 logical array
0 1 1
A(rowsToDelete, :) = [];
A(:, columnsToDelete) = []
A = 1

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgrammatic Model Editing についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by