
<p>When using the while-type do-loop, there is no way to break out of the loop without stopping the program if the size of the matrix is exceeded.</p><p>In the following “programs”, assume the raw data is given as a row matrix Y consisting of numbers that can range from 0 to 100.</p><p>The following scripts can be used with a data file, which will have to be opened. I have such a file called ‘grades.txt’: load grades.txt Y = grades;</p><p>Finding the maximum and minimum without the built in commands:</p><p>% Pick starting values for max and min for later comparison Y_max = Y(1); Y_min = Y(1); % The do loop for k = 2:1:length(Y) if Y(k) > Y_max Y_max = Y(k); elseif Y(k) < Y_min Y_min = Y(k); end end</p><p>Making a frequency vector without using the built in commands:</p><p>% Initialize the frequency vector and create the vector % of unique values range = Y_max - Y_min + 1; Freq = zeros(1,range); Y_unique = [Y_min:1:Y_max]; % The do loop will compute the frequencies of each unique % value of Y by comparing the values in Y to the unique % values, and adding 1 each time it appears for m = 1:1:range for k = 1:1:length(Y) if Y(k) == Y_unique(m) Freq(m) = Freq(m) + 1; end end end</p><p>Simple DO loops and IF statements in MATLAB</p>
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages1 Page
-
File Size-