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 件のコメント

Stephen23
Stephen23 2018 年 10 月 15 日
Use logical indexing.
Shubham Mohan Tatpalliwar
Shubham Mohan Tatpalliwar 2018 年 10 月 15 日
yes i have used logical factors to create the 0 0 0 to convert the unwanted negative or positive values.
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 日

1 投票

if A<=0 & B<=0
...
elseif A<=0 & B>=0
...
elseif A>=0 & B<=0
...
elseif A>=0 & B>=0
...
end

9 件のコメント

Shubham Mohan Tatpalliwar
Shubham Mohan Tatpalliwar 2018 年 10 月 15 日
does this work for vector inputs? because i have thousands of values
Torsten
Torsten 2018 年 10 月 15 日
Yes, A>=0 tests whether all elements of A are nonnegative, e.g.
Shubham Mohan Tatpalliwar
Shubham Mohan Tatpalliwar 2018 年 10 月 15 日
Operands to the and && operators must be convertible to logical scalar values.
it shows this error
Torsten
Torsten 2018 年 10 月 15 日
Why do you use "&&" ? I only used "&".
Shubham Mohan Tatpalliwar
Shubham Mohan Tatpalliwar 2018 年 10 月 15 日
matlab recommended it... will try it to do with only &
Shubham Mohan Tatpalliwar
Shubham Mohan Tatpalliwar 2018 年 10 月 16 日
if the condition does not satisfies the answer should be zero.... for example if 4 satisfies then 1 2 3 would be zzero....
how should i modify my program for this
Torsten
Torsten 2018 年 10 月 17 日
I must admit that I don't fully understand what you mean.
Maybe this
condition = zeros(4,1);
if A<=0 & B<=0
condition(1) = 1;
elseif A<=0 & B>=0
condition(2) = 1;
elseif A>=0 & B<=0
condition(3) = 1;
elseif A>=0 & B>=0
condition(4) = 1
end
?
Best wishes
Torsten.
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 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by