When Using the While-Type Do-Loop, There Is No Way to Break out of the Loop Without Stopping

When Using the While-Type Do-Loop, There Is No Way to Break out of the Loop Without Stopping

<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>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    1 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us