How to loop through all rows using a for loop
Ältere Kommentare anzeigen
I have to create a for loop that retrieve each row and store it in a variable
- ,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 Kommentare
James Tursa
am 23 Apr. 2019
Bearbeitet: James Tursa
am 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
am 23 Apr. 2019
Antworten (1)
James Tursa
am 23 Apr. 2019
Bearbeitet: James Tursa
am 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 Kommentar
Daria Ivanchenko
am 21 Okt. 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?
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!