フィルターのクリア

Finding multiple mean values of matric

11 ビュー (過去 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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by