필터 지우기
필터 지우기

How to check multiple values of one variable in if statement

조회 수: 17 (최근 30일)
Hi,
I am writing a piece of code that checks for zero columns in a matrix. It stores all zero columns it finds in the value col. I later want to check the values for variable col in different if statements. However, when I write it like this it runs into errors:
%find all columns where there is zero vector
col = find(all(R == 0))
if col == [1 2 3]
//does something
end
This code works fine when col has 3 values, but when it has 4 values like col = 1 2 3 4, it gives the error: Matrix dimensions must agree. Is there another way I could check for multiple col values? Thanks!!
  댓글 수: 1
Kevin Phung
Kevin Phung 2019년 2월 1일
편집: Kevin Phung 2019년 2월 1일
you have to be more specific, what is the 'something' that you are doing?

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

채택된 답변

Bob Thompson
Bob Thompson 2019년 2월 1일
편집: Bob Thompson 2019년 2월 1일
You should just be able to index your if condition.
if col(1:3) == [1 2 3]
// do something
end

추가 답변 (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