在MATLAB中从循环创建一个数组?

时间:2015-09-27 00:10:55

标签: arrays matlab loops

这是我的Matlab代码:

Length = length(High);
i = 1;
j = 20;

while i < Length
     HighestHIGH(i) = max(High(i:j));
     i = i+1;
     j = j+1;
end

这会在HighestHIGH行产生错误。我想要实现的是:让我们假设High是一个长度为100的数组(长度= 100)。我希望在新阵列中得到最多的20集。例如:

HighestHIGH[1] = max(High(1:20));
HighestHIGH[2] = max(High(2:21));
HighestHIGH[3] = max(High(3:22));
...
HighestHIGH[80] = max(High(81:100));

1 个答案:

答案 0 :(得分:1)

i==Length-1时,j==Length+18超过High的大小。循环的上限太高了。

我写这个:

N=20;
HighestHIGH=zeros(length(High)-N+1);
for i=1:length(High)-N+1
   HighestHIGH(i) = max(High(i:i+N-1));
end

请注意,根据您的需要,最终字词为HighestHIGH(81)=max(High(81:100))