When Using the While-Type Do-Loop, There Is No Way to Break out of the Loop Without Stopping
Total Page:16
File Type:pdf, Size:1020Kb
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.
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.
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;
Finding the maximum and minimum without the built in commands:
% 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
Making a frequency vector without using the built in commands:
% 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
Simple DO loops and IF statements in MATLAB