2013-01-22 57 views
0

我有一個計算方差的循環,我想繪製它。將for循環的數據存儲到MATLAB中繪圖

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 

如何將差異存儲到數組或矩陣,以便我可以將它們全部繪製出來?

回答

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 
+0

過了一段時間我回到這個辦法。但它的工作。謝謝 – ELSheepO