Array Elements and Equality Checks

1 view (last 30 days)
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 Comments
Joseph Percsy
Joseph Percsy on 5 Apr 2013
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).

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 5 Apr 2013
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).
  1 Comment
Joseph Percsy
Joseph Percsy on 5 Apr 2013
That's exactly what I was looking for, thanks Image Analyst!

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!