multiple if statements in matlab
이전 댓글 표시
% 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
Image Analyst
2012년 12월 16일
After you supply sample/typical values for a_x, p, t, and a, then I will run it. Off the top of my head it looks like this could be vectorized.
Rakesh
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
채택된 답변
추가 답변 (1개)
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
Jürgen
2012년 12월 16일
okay should have noticed that :-)
In MATLAB this statement could easily be true. For example:
>> a_s = 200;
>> a = 1;
>> a_s< a <= a_s-180
ans =
1
Jürgen
2012년 12월 16일
Hey Matt, could you explain that please? I normally use: a_s< a& a <= a_s-180 and that gives as expected ans = 0 so they other statement acts as an "or" operator or what? regards,J
Image Analyst
2012년 12월 16일
Yes that's correct. We see this all the time, and know that often causes problems because that is not what the user was intending when he coded it up. Jurgen you should use && in that situation, because it's a boolean test, not a bit-by-bit operation that you want to do.
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
2012년 12월 16일
ok thanks
카테고리
도움말 센터 및 File Exchange에서 Parallel Computing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!