0

我有一个计算方差的循环,我想绘制它。

for k = [1:3]
disp(B.colheaders{1, k});
%disp(B.data(1:79, k + 1));
for j = [1:10:79]
    disp(j)
    Variance = var(B.data(j:j+8, k))
end
%disp(V)
plot(Variance)
end

如何将方差存储到数组或矩阵中,以便将它们全部绘制出来?

4

1 回答 1

3

如果你想保持循环,这是一种方法:

figure,hold on %# avoid erasing the previous plots
for k = [1:3]
disp(B.colheaders{1, k});
%disp(B.data(1:79, k + 1));
startIdx = [1:10:79];
Variance = zeros(size(startIdx));
for j = startIdx
    disp(j)
    Variance(j==startIdx) = var(B.data(j:j+8, k))
end
%disp(V)
plot(Variance)
end

这是一种跳过内部循环的方法

figure,hold on
for k = [1:3]
disp(B.colheaders{1, k});
%disp(B.data(1:79, k + 1));
startIdx = [1:10:79];
varIdx = bsxfun(@plus,startIdx,(0:7)'); %# create array for indexing

currentData = B.data(:,k);

%# calculate variance for each column
Variance = var(currentData(varIdx),1,1);

plot(Variance)
end
于 2013-01-22T13:25:11.783 回答