Task: Write a script to convert missing values indicated by -999 to NaN in a list of data values in this E6.dat file .
The simplest implementation of an if block is the following test if the single variable A (a scalar) is a positive number.
if A >= 0 % use value of A in calculation B=sqrt(A); else % invalid value for A; mark as missing/inappropriate B=NaN; endwhich does one of two commands depending on the sign of the variable A. That is, the logical test (A >= 0) is true. We will look at general logical tests in the next task.
Clearly, this test might be done on individual members of a list, inside a loop. In this case, A is a list (array).
B=zeros(size(A)); % create an array to hold the answer for i=1:length(A) % test if each element of the list A is non-negative if A(i) >= 0 % valid value of A(i) B(i)=sqrt(A(i)); else % invalid value for A(i); mark as missing/inappropriate B(i)=NaN; end end
It is possible to have more than two possible conditions which can be tested:
if n > 10 disp(' power is too large') B=NaN; elseif n > 0 B = A.^n; elseif n == 0 B = 1; else disp('negative powers are not allowed') B=NaN; endThe code above calculates the powers (from 0 to 10) of a number A. For larger or negative powers, the result is NaN.
Be aware that the order of testing in the loop above is from top to bottom. So if the previous set of commands was arranged as follows, the test for n>10 will never be done, because the first test (n>0) will capture all cases of positive n.
if n > 0 B = A.^n; elseif n > 10 % this section is never executed disp(' power is too large') B=NaN; elseif n == 0 B = 1; else disp('negative powers are not allowed') B=NaN; end
Logical expressions have values of 1 (true) or 0 (false). One use of this is to create logical flags to control whether some action is taken or not. For example, consider the following:
SavePlot=1; % true, save the plot % SavePlot=0; % false, don't save the plot figure plot(t,x) title(' time behavior of x') if SavePlot print('TimePlot.png','-dpng') endThe variable SavePlot can be set to 0 to avoid saving the plot or 1 to save the plot. Similar variables can be used to indicate if some information is available or if some other action should be done or not.
Another example using a logical flag to control calculations is
DoTask=[0 1 1]; % skip task 1 and do tasks 2 and 3 if DoTask(1) % do stuff for task 1 end if DoTask(2) % do stuff for task 2 end if DoTask(3) % do stuff for task 3 endwhich allows you to program different tasks in a single script, but have it do only specific tasks that you want.
A final looping structure (while) is useful if you don't know how many trips through a loop will be required. For example, with the compound interest problem, the question may be "how many years are required to double the initial investment at some interest rate?".
You could solve the problem by just running the original routine for a bunch of years and look for the year that the principle doubles. A better solution is to use a while loop:
r=0.02: % annual interest rate Po=100; % initial investment Target=2*Po; % desired result P=Po; year=1; while (P < Target) P = P + r*P; year=year+1; end disp([' Principle doubles in ' num2str(year) ' years'])The test is checked at the bottom of the loop, so at least one set of calculations will be done.
Also, it is possible for the loop to never end if the stopping test is not properly specified. What would happen if r in the script above is set to zero or a negative value?
Flow chart for task:
%%% Read data file %%% loop through values (assume it is a single column) %%% test for missing indicator %%% convert to NaN
Script for task:
%%% Read data file V=load('E6.dat'); %%% loop through values (assume it is a single column) for i=1:length(V) %%% test for missing indicator if V(i) == -999 %%% if true, convert to NaN V(i)=NaN; end end