这是我的一维数组A,包含10个数字:
A = [-8.92100000000000 10.6100000000000 1.33300000000000 ...
-2.57400000000000 -4.52700000000000 9.63300000000000 ...
4.26200000000000 16.9580000000000 8.16900000000000 4.75100000000000];
我希望循环能够像这样经历; (计算平均间隔) - 间隔长度为2,4,8
(a(1)+a(2))/2 - value stored in one block of a matrix say m= zeros(10)
then (a(1)+a(2)+a(3)+a(4))/4 ------ mean-----
then (a(1)+a(2)..... a(8))/8
然后转移索引:
(a(2)+a(3))/2; - mean
(a(2)+a(3)+a(4)+a(5))/4
(a(2)+a(3)...a(9))/8
SO basically 2^n length interval
%____ my code _____%
A= newArrayy;
sum=0;
storeD = zeros(3,9);
flag=true;
for k=1:9
n=1;
while(true)
if(k+(2^n-1)<10)
meanSum= mean(A(k):A(k+2^n-1))
storeD(n,k)= meanSum;
n=n+1;
else
flag=false;
end
end
端
我需要找到每个区间的标准偏差,协方差和均值,但是我在meanSum线上得到一个错误。
答案 0 :(得分:1)
在meanSum行中,你应该写
A(k:k+2^n-1)
您想要访问从k到k + 2 ^ n-1的元素。因此,您必须为选择操作提供范围。
一些建议:
答案 1 :(得分:0)
A = [-8.92100000000000 10.6100000000000 1.33300000000000 ...
-2.57400000000000 -4.52700000000000 9.63300000000000 ...
4.26200000000000 16.9580000000000 8.16900000000000 4.75100000000000];
sum=0;
storeD = zeros(3,9);
for k=1:9
n=1;
while(1==1)
if(k+(2^n-1)<=10)
meanSum= mean(A(k:k+2^n-1));
storedD(n,k)= meanSum
covVar= cov(A(k:k+2^n-1));
storedC(n,k)= covVar
stdDev= std(A(k:k+2^n-1));
storedS(n,k)= stdDev
n=n+1;
else
break;
end
end
end
所以这很有用:D谢谢你的帮助! while循环中有一个错误,因为我没有使用break来退出循环。