How to loop through all rows using a for loop
이전 댓글 표시
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
James Tursa
2019년 4월 23일
편집: James Tursa
2019년 4월 23일
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
2019년 4월 23일
답변 (1개)
James Tursa
2019년 4월 23일
편집: James Tursa
2019년 4월 23일
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
Daria Ivanchenko
2020년 10월 21일
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?
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!