0

I have a loop calculating the variance, which I want to plot.

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

How do I store the variances to an array or matrix so I can plot them all?

1 Answer 1

3

Here's a way if you want to keep the loop:

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

Here's a way where you skip the inner loop

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
Sign up to request clarification or add additional context in comments.

1 Comment

took a while for me to get back to this. But it worked. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.