Problems with Conditionally execute statements
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi, I'm having problems using if, elseif, and else. I'm trying to execute the statements for a matrix of satelite wind data... I pass from cartesian to polar coordinates and then from radians (on angle theta, the direction of the wind vector) to degrees, and I realized that matlab points the zero towards east in the rose (compass function). I want to make a conversion so that a theta angle of zero (or 360) points north, and 90 ° points east. I wrote this lines of code to do that... the weird thing is that the conditionall statements work when I use single values of theta, but when I run it for the whole matrix, it doesn't work, and it uses THE LAST (ELSE) statement, instead of my ELSEIF.. Any help with that? or any other way to make the conversion that I'm trying to do? Any matlab function to do it? Thanks a lot!
if tm < 0
tm = abs(tm)+90;
elseif tm > 90
tm=360+90-(tm);
else
tm=abs(tm-90);
end
My data matrix (tm, already in degrees):
tm = [-159.3884 -173.0609 -174.9012 -178.0744 164.3687 -179.7163 NaN NaN NaN
-161.7762 -179.5870 173.2305 176.2090 158.6573 177.0568 NaN NaN NaN
-163.5107 -174.3112 165.9708 169.3241 152.4023 169.0777 NaN NaN NaN
-170.2709 174.9133 178.0332 169.5016 150.9017 169.8905 NaN NaN NaN
179.7075 174.8766 168.3010 150.7951 143.1325 NaN NaN NaN NaN
165.8388 165.0565 162.8236 140.7707 NaN NaN NaN NaN NaN
152.2543 147.3183 -169.1261 NaN NaN NaN NaN NaN NaN
133.4060 131.5305 NaN NaN NaN NaN NaN NaN NaN]
댓글 수: 2
Azzi Abdelmalek
2012년 9월 8일
your vector tm contains many values, what do you mean by
if tm<0
do you mean all values<0?
채택된 답변
Matt Fig
2012년 9월 8일
편집: Matt Fig
2012년 9월 8일
for ii=1:numel(tm)
if tm(ii) < 0 % Need to look at each element one-at-a-time!
tm(ii) = 90-tm(ii);
elseif tm(ii) > 90
tm(ii) = 450-(tm(ii));
else
tm(ii) = abs(tm(ii)-90);
end
end
I encourage you to read that article by Loren, and click on the comments she refers to in the first sentence.
추가 답변 (1개)
Azzi Abdelmalek
2012년 9월 8일
편집: Azzi Abdelmalek
2012년 9월 8일
% maybe is that what you need
for k=1:length(tm)
if tm(k)< 0
tm(k) = abs(tm(k))+90;
elseif tm(k) > 90
tm(k)=360+90-tm(k);
else
tm(k)=abs(tm(k)-90);
end
end
참고 항목
카테고리
Help Center 및 File Exchange에서 Reference Applications에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!