連続した数字の平均

18 views (last 30 days)
Keito Endo
Keito Endo on 7 Oct 2021
Edited: Hernia Baby on 8 Oct 2021
a = [ 2 3 4 5 5 5 5 5 6;2 3 4 4 4 4 5 5 6 ].'
の行列で1列目の数字が5つ連続した場合(5が5回連続)、連続した行から3つ前までの2列目の数字を平均する方法はありますか?(2 3 4 の平均)
解決策お願いします。
  1 Comment
Keito Endo
Keito Endo on 7 Oct 2021
a = [ 2 3 4 5 5 5 5 5 6 7 8 8 8 8 8;2 3 4 4 4 4 5 5 6 7 8 7 7 8 8 ].'
とした場合、5が5回連続した場合の平均、8が5回連続した場合の平均をそれぞれ出したいです。

Sign in to comment.

Answers (1)

Hernia Baby
Hernia Baby on 8 Oct 2021
Edited: Hernia Baby on 8 Oct 2021
n個続いたものから、2列目のm行前~1行前の平均を格納しています
xの出し方については こちら を参考にしました
n=5;
m=3;
a = [ 2 3 4 5 5 5 5 5 6 7 8 8 8 8 8;2 3 4 4 4 4 5 5 6 7 8 7 7 8 8 ]';
x = cell2mat(arrayfun(@(t)1:t,diff(find([1 diff(a(:,1)') 1])),'un',0))';
idx = find(x==n) - (n - 1);
for ii = 1:length(idx)
idx2 = idx(ii);
A{ii} = mean(a(idx2-m:idx2-1,2));
end
A
A = 1×2 cell array
{[3]} {[6]}

Categories

Find more on 言語の基礎 in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!