Array Elements and Equality Checks

1 次查看(过去 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 个评论
Joseph Percsy
Joseph Percsy 2013-4-5
My apologies, at the moment I want it to produce one of the three statements for each individual array element, as opposed to the two arrays as a whole (which it is currently doing).

请先登录,再进行评论。

采纳的回答

Image Analyst
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 CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by