Taking Mean Of Specific Rows and Columns Within A Loop

6 ビュー (過去 30 日間)
Daniel Koziel
Daniel Koziel 2019 年 11 月 6 日
コメント済み: Daniel Koziel 2019 年 11 月 6 日
Hello, I have a data matrix of 288000 x 5 and I would like to compute the mean of column 2 in increments of 1200.
Therefore, I want to take the mean from 1:1200 and put it in slot 1 of a new vector. Then take 1201:2400 and put it in slot 2 of the new vector.
I have attempted it and was successful doing it outside of a loop but now that I attempt a loop I am having trouble.
y = 1;
for x = 1 : 1200: x < 288000
Mean(y) = mean(Data([x:(x+1199)],2);
y = y + 1;
end
It works for the first data point when I format it outside a loop like:
Mean(1) = mean(Data([1:1200]),2);
Mean(2) = mean(Data([1201:2400]),2);
and so on...
Thank you!

採用された回答

Richard Brown
Richard Brown 2019 年 11 月 6 日
You don't need a loop. Basic idea:
  • extract column 2.
  • Reshape it into a matrix with columns 1200 tall
  • take the mean of each column
The following code will work (so long as you have a number of rows that is a multiple of 1200)
means = mean(reshape(Data(:, 2), 1200, size(Data, 1)/1200))
  1 件のコメント
Daniel Koziel
Daniel Koziel 2019 年 11 月 6 日
Thank you my friend! I also found it to work inside a loop using:
y = 1;
for x = 1: 1200 : 287999
Min(1,y) = min(Data((x:x+1199),2)); %Calculating Min
Max(1,y) = max(Data((x:x+1199),2)); %Calculating Max
Mean(1,y) = mean(Data((x:x+1199),2)); %Calculating Mean
Var(1,y) = var(Data((x:x+1199),2)); %Calculating Var
y = y + 1;
end

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by