Finding multiple mean values of matric
4 次查看(过去 30 天)
显示 更早的评论
I know this is probably not the hardest question, but I can´t find a proper solution at this moment:
I´ve a matrix M (8000x7) and I want to take one column, e.g. M(:,4) and calculate the mean value of different parts of the column. For example having a mean value of 1:50, then 51:100, 101:150, ... and subsequently storing this new array in a variable.
I know this could be a one-liner and would be very happy if you could help me out.
Thanks!
0 个评论
回答(2 个)
Arthur Roué
2020-3-17
You can use reshape, then mean function.
M_1 = M(:,4)
M_2 = reshape(M, 50, 160) % 160 = 8000/50
M_3 = mean(M_2,1)
0 个评论
Guillaume
2020-3-17
编辑:Guillaume
2020-3-17
If the height of your matrix is a multiple of 50 elements: Simply reshape the column in rows of 50 elements and take the mean across the rows:
assert(mod(size(M, 1), 50) == 0, 'Height is not a multiple of 50')
meanof50elements = mean(reshape(M(:, 4), 50, []), 1); %reshape in rows of 50 elements then take the mean across the rows
If the matrix height is not a multiple of 50, then you have to pad the column with NaNs first, and tell mean to ignore the NaNs:
meanof50elements = mean(reshape([M(:, 4); nan(mod(-size(M, 1), 50), 1)] , 50, []), 1, 'omitnan'); %pad column with NaN to make height a multiple of 50 elements, then reshape into rows of 50 elements and take mean across rows
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!