Array Elements and Equality Checks
    2 次查看(过去 30 天)
  
       显示 更早的评论
    
Another really easy question from me, currently I have this code:
    ADL = [52.0, 109.0, 55.8, 61.6, 18.4, 42.2, 18.2, 9.2, 5.6, 19.0, 49.6, 56.8];
  PER = [131.4, 145.4, 129.0, 93.2, 4.0, 32.4, 0.0, 0.0, 0.0, 43.2, 23.2, 88.6];
  Total = 0;
for i=12
    if ADL(i) > PER(i)
        disp('There was greater rainfall in Adelaide.')
    elseif ADL(i) < PER(i)
        disp('There was greater rainfall in Perth.')
    elseif ADL(i) == PER(i)
        disp('There was the same amount of rainfall in both cities.')
    end
end
Yet it really doesn't like me doing it this way, how do I achieve the results I want in MATLAB?
3 个评论
  Image Analyst
      
      
 2013-4-5
				Regarding your last else, see the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
采纳的回答
  Image Analyst
      
      
 2013-4-5
        If you run this:
ADL = [52.0, 109.0, 55.8, 61.6, 18.4, 42.2, 18.2, 9.2, 5.6, 19.0, 49.6, 56.8];
PER = [131.4, 145.4, 129.0, 93.2, 4.0, 32.4, 0.0, 0.0, 0.0, 43.2, 23.2, 88.6];
for k = 1 : length(PER)
  fprintf('For year #%2d, ', k);
  if ADL(k) > PER(k)
    fprintf('there was greater rainfall in Adelaide (%.1f) than in Perth (%.1f).\n',...
      ADL(k), PER(k));
  elseif PER(k) > ADL(k)
    fprintf('there was greater rainfall in Perth (%.1f) than in Adelaide (%.1f).\n',...
      PER(k), ADL(k));
  else
    % Equal rainfall, but may never be totally equal due to FAQ.
    fprintf('there was equal rainfall in Perth (%.1f) and Adelaide (%.1f).\n',...
      PER(k), ADL(k));
  end  
end
is this what you expect?
For year # 1, there was greater rainfall in Perth (131.4) than in Adelaide (52.0).
For year # 2, there was greater rainfall in Perth (145.4) than in Adelaide (109.0).
For year # 3, there was greater rainfall in Perth (129.0) than in Adelaide (55.8).
For year # 4, there was greater rainfall in Perth (93.2) than in Adelaide (61.6).
For year # 5, there was greater rainfall in Adelaide (18.4) than in Perth (4.0).
For year # 6, there was greater rainfall in Adelaide (42.2) than in Perth (32.4).
For year # 7, there was greater rainfall in Adelaide (18.2) than in Perth (0.0).
For year # 8, there was greater rainfall in Adelaide (9.2) than in Perth (0.0).
For year # 9, there was greater rainfall in Adelaide (5.6) than in Perth (0.0).
For year #10, there was greater rainfall in Perth (43.2) than in Adelaide (19.0).
For year #11, there was greater rainfall in Adelaide (49.6) than in Perth (23.2).
For year #12, there was greater rainfall in Perth (88.6) than in Adelaide (56.8).
更多回答(0 个)
另请参阅
类别
				在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


