Array Elements and Equality Checks

조회 수: 2 (최근 30일)
Joseph Percsy
Joseph Percsy 2013년 4월 5일
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).
  댓글 수: 1
Joseph Percsy
Joseph Percsy 2013년 4월 5일
That's exactly what I was looking for, thanks Image Analyst!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by