How to loop through all rows using a for loop

2 views (last 30 days)
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
  2 Comments
James Tursa
James Tursa on 23 Apr 2019
Edited: James Tursa on 23 Apr 2019
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
Miasia Johnson
Miasia Johnson on 23 Apr 2019
I just edited the question so it contains the question from my homework assignment

Sign in to comment.

Answers (1)

James Tursa
James Tursa on 23 Apr 2019
Edited: 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
end
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.
  1 Comment
Daria Ivanchenko
Daria Ivanchenko on 21 Oct 2020
Hi! Could you go further with it please? I have a very similar problem but I don't know how to finish a code. Would you be so kind to do it?

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!