find a row with all elements satisfying a condition

1 回表示 (過去 30 日間)
Alon Osovsky
Alon Osovsky 2020 年 6 月 24 日
コメント済み: Alon Osovsky 2020 年 6 月 24 日
I have a matrix that looks something like this:
mat = [20, 3; 43 0; 8 3; 100 3; 3 9];
I want to find the rows of the matrix that all of its elements are satisfying a certain condition. For example, if the condition is:
> 10
The result matrix will be:
[8 3; 3 9];
Because those are the only rows in the matrix that both of their elements are smaller then 10. Is there an easy way to achieve this result? Thanks!

回答 (1 件)

Aakash Mehta
Aakash Mehta 2020 年 6 月 24 日
編集済み: Aakash Mehta 2020 年 6 月 24 日
mat = [20, 3; 43 0; 8 3; 100 3; 3 9];
You can use the below code to get rows which satisy the condition.
size1 = size(mat);
vec = zeros(size1(1),1);
for i = 1:size1(1)
vec(i) = all(mat(i,:)<10);
end
extract those rows in the resultant matrix.
result = mat(vec==1, :);
  1 件のコメント
Alon Osovsky
Alon Osovsky 2020 年 6 月 24 日
I'm searching for a simpler solution (using already existing functions), without a for loop.

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by