필터 지우기
필터 지우기

Test every element in matrix

조회 수: 7 (최근 30일)
Leor Greenberger
Leor Greenberger 2011년 9월 19일
I have an M x N matrix, where I want to test whether each element is greater than some constant a, and if it is, perform some arithmetic to it. The only way I can think of doing this is using 2 For loops nested to select each element and an If statement to perform checking. Is this the best I can do?

채택된 답변

Tigersnooze
Tigersnooze 2011년 9월 19일
You can say something like this, instead of using a loop (assuming the matrix is called A):
A(A > a) = A(A > a) + arithmetic.
"Vectorizing" like this won't have significant speed advantages with smaller matrices, but when the matrix gets bigger you'll see huge time savings. Just a heads up for the future.
  댓글 수: 4
Sean de Wolski
Sean de Wolski 2011년 9월 19일
Leor, look at ismember:
doc ismember
Leor Greenberger
Leor Greenberger 2011년 9월 19일
Thank you, sir!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by