MATLAB Answers


How to loop through all rows using a for loop

Asked by Miasia Johnson on 23 Apr 2019
Latest activity Edited by James Tursa
on 23 Apr 2019
I have to create a for loop that retrieve each row and store it in a variable
  1. ,create a for loop that goes from one to the number of patients (use the size command to determine the number of rows and thus the number of patients) and loop through the rows of the matrix containing the patient heart rate data. Inside your for loop, use indexing and the loop variable to retrieve each row of your patient heart rate data and store it in a variable. Take the average of this variable using the mean function and store this average value in the appropriate location in AverageHR. For instance, if you retrieve the first row, which corresponds to patient1, store the average in the first element of AverageHR, etc.
I created the matrix its 5 rows and 4 columns


Why do you think you need to do this? This is usually a very bad programming practice that leads to downstream coding problems. Better is to just access the rows by indexing. E.g., for a 2D matrix:
your_matrix(k,:) is the kth row
I just edited the question so it contains the question from my homework assignment

Sign in to comment.

1 Answer

Answer by James Tursa
on 23 Apr 2019
Edited by James Tursa
on 23 Apr 2019

I will get you started:
patients = the variable that has all of the patient information
for k=1:_______ <-- you fill this in according to the instructions using the size function
% You insert code here to store patients(k,:) into a variable
% You insert code here to take the mean of that variable and store it in the appropriate spot of AverageHR
Make an effort to write the code as indicated above. If you have problems, post some follow-up comments here and we can work with you on fixing them.


Sign in to comment.