Plotting using a for loop in Matlab and finding the highest three consecutive value, given imported spreadsheet table of 1x100 called 'P'?

2 次查看(过去 30 天)
for n = 1:100
PP = P(n,1);
x = n;
plot(x, P);
hold on
end
The code above doesn't work. Please help fix it and make sure to display three consecutive values that have the largest sum.

回答(2 个)

Voss
Voss 2021-12-23
编辑:Voss 2021-12-23
P = randn(1,8);
display(P);
P = 1×8
0.9215 -0.2768 0.7009 1.2080 0.0836 -0.7582 1.4873 -0.3553
Npts = 3;
[~,idx] = max(movsum(P,Npts,'EndPoints','discard'));
display(P(idx+(0:Npts-1)));
0.7009 1.2080 0.0836

Dongyue
Dongyue 2022-11-22
Hi, the followed code could be easier to understand:
P = rand(100,1);
mx = -Inf;
for i = 1:98
mx = max(mx, sum(P(i:i+2)));
end
mx

类别

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