function [A] = cesaro(x,N,L,v)
%Soveltaa lukujonoon Cesaron menetelmää
%[A] = cesaro(x,N,L,v)
%x lukujono
%N kertaa
%L pituus
%v valinta: 1=Cesaro, 2=käänteinen Cesaro
%
%n=1:20;
%s=(-1).^(n-1).*(2*n-1);
%A=cesaro(s,3,10,1)
%B=cesaro(s,1,10,2)

if v==1,
A=zeros(L,3+2*N);
A(:,1)=1:L;
A(:,2)=1./A(:,1);
A(:,3)=x(1:L);
for k=1:N
    for j=1:L
    A(j,2*k+2)=sum(A(1:j,2*k+1));
    A(j,2*k+3)=mean(A(1:j,2*k+1));
    end
end
h=figure;hold on
for k=0:N
a=[1 0 1]*(1-(k/N))+[0 1 0]*(k/N);
plot(A(:,1),A(:,2*k+3),'Color',a)
end
elseif v==2,
A=zeros(L,3+2*N);
A(:,1)=1:L;
A(:,2)=1./A(:,1);
A(:,3)=x(1:L);
for k=1:N
    A(:,2*k+2)=A(:,1).*A(:,2*k+1);
    A(:,2*k+3)=[A(1,2*k+2);A(2:L,2*k+2)-A(1:L-1,2*k+2)];
end
h=figure;hold on
for k=0:N
a=[1 0 1]*(1-(k/N))+[0 1 0]*(k/N);
plot(A(:,1),A(:,2*k+3),'Color',a)
end
end
end

