how to calculate the average?
19 views (last 30 days)
I have a matrix that has a dimension of 200*59. I want to calculate the average of the first 6 rows for each column to get 1*59
I've written this loop
I am not sure if the loop is correct or not!
any help is appreciated
Kevin Holly on 19 Oct 2021
Edited: Kevin Holly on 19 Oct 2021
You do not need to create a for loop (see vectorization)
I would also suggest using the mean function instead.
temp = rand(200,59);%random matrix for demonstration purposes
Tavg = mean(temp(1:6,:)) %The first input is a vector of the rows 1 through 6 and the second input is a colon (:), which includes all elements (in this case all columns).
if you have NaN values that you want to ignore:
Tavg = mean(temp(1:6,:),'omitnan')