필터 지우기
필터 지우기

how to formulate if statement for a vector?

조회 수: 1 (최근 30일)
Shubham Mohan Tatpalliwar
Shubham Mohan Tatpalliwar 2018년 10월 15일
댓글: Shubham Mohan Tatpalliwar 2018년 10월 18일
A=[-3,-2,-1,1,2,3]
B=[-3,-2,-1,2,3,4]
i have 4 condition and have to slect only one when it satisfies.
  1. negative A negative B i.e. A=[-3,-2,-1,0,0,0] B=[-3,-2,-1,0,0,0]
  2. negative A positive B .
  3. positive A negative B.
  4. positive A positive B.
How can i select only one condition if input is in vector.
  댓글 수: 4
Steven Lord
Steven Lord 2018년 10월 15일
The elements at some of the locations of your vectors satisfy your first condition. Others satisfy the fourth condition. Do you want to choose one of those conditions at random and apply it to the entire A and B vectors, or do you want to apply the condition that each pair of corresponding elements satisfies to that element of the result?
What do you want to do if none of the conditions are satisfied?
A = zeros(1, 6);
The elements of A are neither positive nor negative.
Shubham Mohan Tatpalliwar
Shubham Mohan Tatpalliwar 2018년 10월 16일
each pair of corresponding elements satisfies to that element of the result?
What do you want to do if none of the conditions are satisfied?
it would not happen.
also if the one condition does not satisfy the answer would be zero

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

채택된 답변

Torsten
Torsten 2018년 10월 15일
편집: Torsten 2018년 10월 15일
if A<=0 & B<=0
...
elseif A<=0 & B>=0
...
elseif A>=0 & B<=0
...
elseif A>=0 & B>=0
...
end
  댓글 수: 9
Jan
Jan 2018년 10월 17일
If A and B are vectors, the expression
if A<=0 & B<=0
is converted internally to:
if all(A(:)<=0 & B(:)<=0) & ~isempty(A) & ~isempty(B)
Writing this explicitly is less confusing in my opinion. Array-valued conditions of if commands are a frequent source of bugs.
Shubham Mohan Tatpalliwar
Shubham Mohan Tatpalliwar 2018년 10월 18일
Can i also use it for 3 variables?

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

추가 답변 (0개)

카테고리

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