필터 지우기
필터 지우기

Find a row with all elements satisfying a condition

조회 수: 10 (최근 30일)
Alon Osovsky
Alon Osovsky 2020년 6월 24일
댓글: mariajose gomez 2021년 7월 13일
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!

채택된 답변

Rasul Khan
Rasul Khan 2020년 6월 24일
You can achieve it with a loop. The required matrix will be stored in res
res = []
for i = 1 : size(mat , 1)
if ~any( mat(i , :) > 10 ) % check for the condition , here if any element is > 10, discard the row
res = [res ; mat(i , :)];
end
end

추가 답변 (1개)

darova
darova 2020년 6월 24일
Use logical indexing
mat = [20, 3; 43 0; 8 3; 100 3; 3 9]
ix = sum(mat<10,2)>1;
a1 = mat(ix,:)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by