Plotting the variance. Please help fix my code

4 views (last 30 days)
Hi all, I currently have a graph of a trace which shows great fluctuations. Therefore, i would like to plot the variance of this trace using a defined window length. The initial trace is a plot of coordinates against force. The coordinates along the x-axis go from 0 to 300 and there are 73500 points. Therefore i would like to plot the variance of the force for xcoordinates 0 to 1000, plot this, calculate the variance from 1000 to 2000, plot this and so on until the end.
My code so far is
function[X1,Y1,X2,Y2,c,c1]=SMPcomparison(points1,points2,day)
fname1 = points1; % relates the user inputted file to the variable fname(n)
fname2 = points2;
points1=load (points1); % loads the matrix associated with user inputted variable into the workspace
points2 = load(points2);
X1 = points1(:,1);% takes respective column of matrix and correlates this with either an X or Y coordinate
Y1 = points1(:,2);
X2 = points2(:,1);
Y2 = points2(:,2);
subplot(1,1,1),plot(X1,Y1,'g',X2,Y2,'r') % plots data sets
N=length(X1);
x=1
y=1
i = 1;
j=1000;
n= length(Y1);
while i<n-1000
for c = i:1000:n
v = var(Y1(i:j))
i = i+1000;
j=i+1000
variance{y,2} = v
y=y+1;
if j>=n-1000;
break
%end
%end
%record the data to an output matrix
% subplot(2,1,2),plot(X1 ,v)
end

Answers (1)

Oleg Komarov
Oleg Komarov on 27 Mar 2011
You can use this approach:
% Supose you have an array of 73500 elements and you want to calculate the
% variance on non overlapping windows of 1000 elements
A = rand(73500,1);
% I leave out the last 500 elements
B = A(1:end-mod(numel(A),1000));
% Reshape B into 1000 by 73 and calculate the variance on the 73 windows
Out = var(reshape(B,1000,numel(B)/1000),2);

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!