multiple if statements in matlab

3 ビュー (過去 30 日間)
Rakesh
Rakesh 2012 年 12 月 16 日
% inputs are a_s, p, t, a
% a_s=single number
% p,t,a are (nX1) column vectors each
% output is P (also a column vector of dimension (nX1))
%---------actual equations--------
if a_s<a<=a_s-180
P=p+t for p<=180-t
P=p+t-180 for p>180-t
if a<=a_s or a_s-180<a
P=p-t for p>=t
P=p-t+180 for p<t
Note: The two if's are connected.
%---Matlab prog. for above eqn.---
if a_s<a<=a_s-180
if p<=180-t %------(1)
P=p+t;
elseif p>180-t %------(2)
P=p+t-180;
end
elseif a<=a_s | a_s-180<a
if p>=t %------(3)
P=p-t;
elseif p<t %------(4)
P=p-t+180;
end
end
% I couldn't figure the mistake. While running the program, (1)&(3) are
% ignored in matlab.
  3 件のコメント
Rakesh
Rakesh 2012 年 12 月 16 日
Thanks for the reply. Here are few values of the variables:
a=[10 11 55 22 25 34 39 40 41 52 54 76 113 125 133];
p=[40 51 40 22 45 19 97 79 135 222 175 138 125 77 20]';
t=[80 75 45 71 71 63 61 72 89 78 73 54 9 54 65]';
a_s=283;
Jürgen
Jürgen 2012 年 12 月 16 日
編集済み: Jürgen 2012 年 12 月 16 日
as said above the code works fine , but see my answer: I still wonder what you want to do, you gave us a column vectors a, p and t so with those inputs and the provided code you can only run the program and see that none of your conditions are fullfilled, unless if you want to run it elementwise then you need to add a for loop for indexing, but in that case you can calculate it directly with the vector, e.g. by using the result of comparing a to a_s (zero and ones) to multiply with -t and +t

サインインしてコメントする。

採用された回答

Jürgen
Jürgen 2012 年 12 月 16 日
編集済み: Jürgen 2012 年 12 月 16 日
Hey, difficult to check it without example values but if I understand it well you are comparing a column vector p to a column t, so then all element needs to be larger or smaller than the ones in the other column no? or do you want to compare element wise

その他の回答 (1 件)

Image Analyst
Image Analyst 2012 年 12 月 16 日
How can this ever be true:
a_s< a <= a_s-180
A number can't be greater than a certain number and also less than the number, and certainly not less that that number minus 180!
  6 件のコメント
Matt Fig
Matt Fig 2012 年 12 月 16 日
編集済み: Matt Fig 2012 年 12 月 16 日
MATLAB evaluates left to right. So as you wrote it, the expression evaluates this way
(a_s< a) <= a_s-180
What you probably meant was
a_s< a && a<= a_s-180
But then IA's original comment applies.
Jürgen
Jürgen 2012 年 12 月 16 日
ok thanks

サインインしてコメントする。

カテゴリ

Help Center および File ExchangePrice and Analyze Financial Instruments についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by