Finding multiple mean values of matric

조회 수: 15 (최근 30일)
Robin Strak
Robin Strak 2020년 3월 17일
편집: Guillaume 2020년 3월 17일
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!

답변 (2개)

Arthur Roué
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)

Guillaume
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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by