Array Elements and Equality Checks
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Image Analyst
le 5 Avr 2013
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
Réponse acceptée
Image Analyst
le 5 Avr 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).
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!