Hi,
I am struggling how to extract part of a 2D matrix based on logical criteria:
example:
a=[1 1 1 ; 2 2 2 ; 3 3 3]
b=a<3 % let say I want to have the subset where a<3
mySubset=a.*b
how can I obtain the subset using logic and not having a line of zeros where a=3?
Thanks

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 9월 5일
편집: Azzi Abdelmalek 2013년 9월 5일

0 개 추천

a=[1 1 1 ; 2 2 2 ; 3 3 3]
b==a<3
mySubset=a.*b

댓글 수: 4

Simon
Simon 2013년 9월 5일
Thanks for the answer, however, the resulting subset is still of the same dimension as "a". I would like to end up in this example with a matrix containing only 1 and 2, meaning a 2*3 matrix.
a=a(all(a<3,2),:)
Simon
Simon 2013년 9월 5일
Thanks! I was not aware of the function all()
The 'all' method only works if all elements of a row in 'a' are less than 3. In general I think what you really want is
mySubset = a(b);
The result will be a one-dimensional array but in the general case that is unavoidable.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기

제품

태그

Community Treasure Hunt

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

Start Hunting!

Translated by