我有一个连续变量A=gamrnd(5,0.4,1000,28)
和一个输出变量Y=lognrnd(7,1.9,1000,28)
A
7.6472 3.4284 6.3352 8.0480 8.1021
12.3371 5.1611 6.3986 9.3687 9.5652
8.7700 5.2980 6.0307 2.8651 12.6011
12.2042 4.5636 6.0570 7.1348 8.6586
7.8960 5.5213 3.7105 6.4875 7.4891
Y
1.9733 14.0951 14.0951 14.0951 14.0951
9.4284 11.7573 15.6730 25.4495 24.6680
3.4724 4.4953 7.1237 9.4191 18.4504
8.9548 8.9548 8.9548 8.9548 8.9548
1.4834 2.5393 2.5393 2.5393 2.5393
我想针对该红色框的特定A(或宽度)值计算variance
Y
?假设我将A
的域分成20个红色框,我想为每个框计算Y
的方差。也就是说:
$ VAR(Y | A = A_I)$
有没有想过我会怎么做?
到目前为止我的想法:
[i j]=find(9.5<=A & A<=10.5)
sig=var(reshape((Y([i j])),length(i)*2,1))
但这是正确的,而是临时的。假设我在A中有一百个分区。是否可以使用更高效的东西?
答案 0 :(得分:1)
accumarray
救援!
%# split A into 100 chunks
nChunks = 100;
Aidx = round(A/maxA*(nChunks-1))+1;
%# get the number of data points just in case
nDataPoints = histc(Aidx,1:nChunks);
%# calculate the variance
varA = accumarray(Aidx,Y,[100,1],@var,NaN);