Task: Write a script to read the Norfolk monthly mean temperature NorfolkMonMeanTemp.dat and count how many times the august mean temperature is above 80F. The data cover years from 1874 to 2018. There are 3 data columns: year, month, temperature (F).
There are six numerical comparison operators:
> tests " A greater than B" A > B >= tests "A greater than or equal to B" A >= B < tests "A less than B" A < B <= tests "A less than or equal to B" A <= B == tests "equal to" A == B ~= tests "A not equal to B" A ~= BThere are three logical operators:
& means "and" The test is true only if both of the logical variables are true. Otherwise, the test is false. A & B | means "or" The test is true if either or both of the logical variables are true. Otherwise, the test is false. A | B ~ means "not" or "negate" Change true to false or false to true. ~ A
These operators can be combined in various ways to control actions in MATLAB.
Consider two vectors A and B which are the same size. We can create a test result (a logical variable) that represents some comparison of the values of these variables.
L = A >= B;Then the vector L will be the same length as A and B with values of 0 (false) or 1 (true) indicating the comparison of the size of the corresponding values.
This information is useful, but we may want to know at which locations in these arrays this comparison is true. The find command returns the index of locations in a logical array for which the test is true. For example,
A = [-2 -1 0 1 2]; L = A > 0; % the result is L = [0 0 0 1 1] p=find(A > 0); % the result is p = [4 5]The tests can be more complicated by combining several logical tests.
% find positive values of A that are % greater than corresponding values of B G=find( (A > B ) & ( A > 0)); % find values where either A or B or both are negative N=find( (A < 0 ) | ( B < 0));
The find command works a bit differently with matrices (tables). Consider a 2-D array of land elevations: Elev(nlat,nlon) with associated location arrays lat(nlat) and lon(nlon). Suppose you want to find the locations where elevation is above some value. The command
i=find(Elev > Target);will return a linear index in the array for the desired values. That is, a running index down successive rows of Elev. If you try to find the locations, lat(i) and lon(i) will not work because these arrays have a different size and shape from Elev.
What you want is for find to return both row and column indexes of the points of interest in a table. Allowing two output variables informs the command that you want the row and column indexes. The command is
[r c]=find(Elev > Target];Now lat(r) and lon(c) will return the desired locations of the target points.
Flow chart for the task:
%%% read data file %%% rename input variables %%% setup up a counter %%% loop through all temperatures %%% test for august and T >= 80F %%% increase counter if tests are true %%% display the counter
Script to complete the task:
%%% read data file data=load('NorfolkMonMeanTemp.dat'); %%% rename input variables year=data(:,1);month=data(:,2);tempF=data(:,3); %%% setup up a counter count80=0; %%% loop through all temperatures for it=1:length(tempF) %%% test for T >= 80F if (tempF(it) >= 80 & month(it) == 8) %%% increase counter if tests are true count80=count80+1; end end %% a faster test is %% look for all locations in the list where both conditions are true % i80=find(tempF >= 80 & month == 8); % count80=length(i80); %% or even shorter by combining the above two commands % count80=length(find(tempF >= 80 & month == 8)); %%% display the counter disp(['Number of months for temp > 80F is ' num2str(count80)])