Take the 2-minute tour ×

The maximum daily temperature (in F) for Chicago and San Francisco during the month of August 2009 are given in the vectors below (data from the U.S. National Oceanic and Atmospheric Administration).

TCH = [75  79  86  86  79  81  73  89  91  86  81  82  86  88  89  90  82  84  81
79  73  69  73  79  82  72  66  71  69  66  66]
TSF = [69  68  70  73  72  71  69  76  85  87  74  84  76  68  79  75  68  68  73
72  79  68  68  69  71  70  89  95  90  66  69]

Write a program in a script file to answer the following:

(c) How many days, and on which dates in the month, was the temperature in San Francisco lower than the temperature in Chicago? (d) How many days, and on which dates in the month, was the temperature the same in both cities?

I can't figure out how to approach this. PLEASE HELP!! EDIT: I can show how many days for each part but can't figure out how to associate the dates with the temperature arrays.

share|improve this question

closed as not a real question by natan, gnat, bensiu, Yan Sklyarenko, SWeko Feb 15 '13 at 7:59


Is this for a class? If so, whatever introductory material you were given really ought to have enough info in it to figure this out, and you'd be poorly served to get the answer here. –  tmpearce Feb 15 '13 at 2:32
Hint: You could use a loop (since it is in the title) but you don't need to. What have you tried? –  tmpearce Feb 15 '13 at 2:33
For (c), I created a for loop using count and it came out right. The problem is the part where I have to display the dates for those days. Can't figure out how to bring them together. –  Bob S Feb 15 '13 at 2:49
add comment

2 Answers

This question is possible without loops.

Without just giving you the answer, look into the find function. Put a conditional (boolean) statement within the brackets, and this will give you the indexes for which that conditional holds.

Take a look at how conditionals behave between two arrays:

a = [1 2 3 4 5];
b = [5 4 3 2 1];
x=(a == b); //gives [false false true false false]

From there, find(x) returns all indexes where x[index] is nonzero (i.e. not false). In this example, find(x) (or find(a==b)) would return [3].

share|improve this answer
add comment
sameDates = find(TCH==TSF);
lessDates = find(TSF < TCH);

fprintf(1, 'it was the same on %d days\n', numel(sameDates));
fprintf(1, 'it was lower on %d days\n', numel(lessDates));

for ii = sameDates
    fprintf(1, 'same temperature of %d on %d August\n', TCH(ii), ii)
share|improve this answer
While possibly correct, this answer isn't really helping anyone to learn and is not doing anyone any favours. –  Bill Cheatham Feb 15 '13 at 12:36
@BillCheatham - I understand your point. Here is mine: "Somebody asked for help, I give it." To say "you should not ask for my help", even if that would "help them better" in the long run, is not treating the OP as a grownup. I don't know the circumstances of the person asking for help; I am assuming he has weighed the pros and cons of asking vs figuring it out himself. I am not responsible for his education. I think it's a matter of personal style. –  Floris Feb 15 '13 at 13:52
add comment
