Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
sum of exponentials

Subject: sum of exponentials

From: dwi

Date: 22 Nov, 2012 12:47:07

Message: 1 of 10

I have a matrix whose data are interrupted by sequences of zeros. I need every time that there's a zero value to substitute it with a sum of exponentials using the previous data, eg:
x=[x1 x2 x3 0 0 0 x7 0 0]
When i find the first zero in the element x4 I want:
x4=(x3*e^(-1)+x2*e^(-2)+x1*e^(-3))/(e^(-1)+e^(-2)+e^(-3));
However, when I find the second zero value I need to calculate the same expression but without using the previous recalculated values. That is,
x4=x5=x6
and for x8 I will use only the values in x7,x3,x2,x1 and then x8=x9 etc
And all this for a 180000-length data.
Any ideas on how to do this?
Thanks in advance

Subject: sum of exponentials

From: Roger Stafford

Date: 22 Nov, 2012 18:19:08

Message: 2 of 10

"dwi" wrote in message <k8l6sb$2fj$1@newscl01ah.mathworks.com>...
> I have a matrix whose data are interrupted by sequences of zeros. I need every time that there's a zero value to substitute it with a sum of exponentials using the previous data, eg:
> x=[x1 x2 x3 0 0 0 x7 0 0]
> When i find the first zero in the element x4 I want:
> x4=(x3*e^(-1)+x2*e^(-2)+x1*e^(-3))/(e^(-1)+e^(-2)+e^(-3));
> However, when I find the second zero value I need to calculate the same expression but without using the previous recalculated values. That is,
> x4=x5=x6
> and for x8 I will use only the values in x7,x3,x2,x1 and then x8=x9 etc
> And all this for a 180000-length data.
> Any ideas on how to do this?
> Thanks in advance
- - - - - - - - -
 a = 0; b = 0;
 for k = 1:length(x)
   if x(k) ~= 0
     a = x(k) + a*e^(-1);
     b = 1 + b*e^(-1);
   else
     x(k) = a/b;
   end
 end

Roger Stafford

Subject: sum of exponentials

From: dwi

Date: 23 Nov, 2012 14:54:08

Message: 3 of 10

"Roger Stafford" wrote in message <k8lqar$4po$1@newscl01ah.mathworks.com>...
> "dwi" wrote in message <k8l6sb$2fj$1@newscl01ah.mathworks.com>...
> > I have a matrix whose data are interrupted by sequences of zeros. I need every time that there's a zero value to substitute it with a sum of exponentials using the previous data, eg:
> > x=[x1 x2 x3 0 0 0 x7 0 0]
> > When i find the first zero in the element x4 I want:
> > x4=(x3*e^(-1)+x2*e^(-2)+x1*e^(-3))/(e^(-1)+e^(-2)+e^(-3));
> > However, when I find the second zero value I need to calculate the same expression but without using the previous recalculated values. That is,
> > x4=x5=x6
> > and for x8 I will use only the values in x7,x3,x2,x1 and then x8=x9 etc
> > And all this for a 180000-length data.
> > Any ideas on how to do this?
> > Thanks in advance
> - - - - - - - - -
> a = 0; b = 0;
> for k = 1:length(x)
> if x(k) ~= 0
> a = x(k) + a*e^(-1);
> b = 1 + b*e^(-1);
> else
> x(k) = a/b;
> end
> end
>
> Roger Stafford
Thank you for your answer but if I use this the index of the exponent never changes.I want it to decrease as k increases, and i don't know hot to calculate a sum like this.

Subject: sum of exponentials

From: dwi

Date: 23 Nov, 2012 15:16:08

Message: 4 of 10

"dwi" wrote in message <k8o2mg$pdp$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <k8lqar$4po$1@newscl01ah.mathworks.com>...
> > "dwi" wrote in message <k8l6sb$2fj$1@newscl01ah.mathworks.com>...
> > > I have a matrix whose data are interrupted by sequences of zeros. I need every time that there's a zero value to substitute it with a sum of exponentials using the previous data, eg:
> > > x=[x1 x2 x3 0 0 0 x7 0 0]
> > > When i find the first zero in the element x4 I want:
> > > x4=(x3*e^(-1)+x2*e^(-2)+x1*e^(-3))/(e^(-1)+e^(-2)+e^(-3));
> > > However, when I find the second zero value I need to calculate the same expression but without using the previous recalculated values. That is,
> > > x4=x5=x6
> > > and for x8 I will use only the values in x7,x3,x2,x1 and then x8=x9 etc
> > > And all this for a 180000-length data.
> > > Any ideas on how to do this?
> > > Thanks in advance
> > - - - - - - - - -
> > a = 0; b = 0;
> > for k = 1:length(x)
> > if x(k) ~= 0
> > a = x(k) + a*e^(-1);
> > b = 1 + b*e^(-1);
> > else
> > x(k) = a/b;
> > end
> > end
> >
> > Roger Stafford
> Thank you for your answer but if I use this the index of the exponent never changes.I want it to decrease as k increases, and i don't know hot to calculate a sum like this.

A correction:
x8=(x7*e^(-1))/e^(-1)
ie without using x1,x2,x3 but only the immediate previous non-zero values

Subject: sum of exponentials

From: dwi

Date: 23 Nov, 2012 15:48:08

Message: 5 of 10

"dwi" wrote in message <k8o3vo$ia$1@newscl01ah.mathworks.com>...
> "dwi" wrote in message <k8o2mg$pdp$1@newscl01ah.mathworks.com>...
> > "Roger Stafford" wrote in message <k8lqar$4po$1@newscl01ah.mathworks.com>...
> > > "dwi" wrote in message <k8l6sb$2fj$1@newscl01ah.mathworks.com>...
> > > > I have a matrix whose data are interrupted by sequences of zeros. I need every time that there's a zero value to substitute it with a sum of exponentials using the previous data, eg:
> > > > x=[x1 x2 x3 0 0 0 x7 0 0]
> > > > When i find the first zero in the element x4 I want:
> > > > x4=(x3*e^(-1)+x2*e^(-2)+x1*e^(-3))/(e^(-1)+e^(-2)+e^(-3));
> > > > However, when I find the second zero value I need to calculate the same expression but without using the previous recalculated values. That is,
> > > > x4=x5=x6
> > > > and for x8 I will use only the values in x7,x3,x2,x1 and then x8=x9 etc
> > > > And all this for a 180000-length data.
> > > > Any ideas on how to do this?
> > > > Thanks in advance
> > > - - - - - - - - -
> > > a = 0; b = 0;
> > > for k = 1:length(x)
> > > if x(k) ~= 0
> > > a = x(k) + a*e^(-1);
> > > b = 1 + b*e^(-1);
> > > else
> > > x(k) = a/b;
> > > end
> > > end
> > >
> > > Roger Stafford
> > Thank you for your answer but if I use this the index of the exponent never changes.I want it to decrease as k increases, and i don't know hot to calculate a sum like this.
>
> A correction:
> x8=(x7*e^(-1))/e^(-1)
> ie without using x1,x2,x3 but only the immediate previous non-zero values

This is what I have so far:
s=0;
s1=0;
i=1;
while i<=length(x)
for j=1:length(x)
if x(i)~=0
s=s+???? %%%%%the numerator which I still don't know how to calculate
s1=s1+e^(-j);
i=i+1;
else
x(i)=s/s1;
i=i+1;
j=1;
s=0;
s1=0;
end
end
end
This seems to work fine for the denominator. ANy ideas about the numerator?

Subject: sum of exponentials

From: dwi

Date: 23 Nov, 2012 17:01:07

Message: 6 of 10

"dwi" wrote in message <k8o5ro$6ae$1@newscl01ah.mathworks.com>...
> "dwi" wrote in message <k8o3vo$ia$1@newscl01ah.mathworks.com>...
> > "dwi" wrote in message <k8o2mg$pdp$1@newscl01ah.mathworks.com>...
> > > "Roger Stafford" wrote in message <k8lqar$4po$1@newscl01ah.mathworks.com>...
> > > > "dwi" wrote in message <k8l6sb$2fj$1@newscl01ah.mathworks.com>...
> > > > > I have a matrix whose data are interrupted by sequences of zeros. I need every time that there's a zero value to substitute it with a sum of exponentials using the previous data, eg:
> > > > > x=[x1 x2 x3 0 0 0 x7 0 0]
> > > > > When i find the first zero in the element x4 I want:
> > > > > x4=(x3*e^(-1)+x2*e^(-2)+x1*e^(-3))/(e^(-1)+e^(-2)+e^(-3));
> > > > > However, when I find the second zero value I need to calculate the same expression but without using the previous recalculated values. That is,
> > > > > x4=x5=x6
> > > > > and for x8 I will use only the values in x7,x3,x2,x1 and then x8=x9 etc
> > > > > And all this for a 180000-length data.
> > > > > Any ideas on how to do this?
> > > > > Thanks in advance
> > > > - - - - - - - - -
> > > > a = 0; b = 0;
> > > > for k = 1:length(x)
> > > > if x(k) ~= 0
> > > > a = x(k) + a*e^(-1);
> > > > b = 1 + b*e^(-1);
> > > > else
> > > > x(k) = a/b;
> > > > end
> > > > end
> > > >
> > > > Roger Stafford
> > > Thank you for your answer but if I use this the index of the exponent never changes.I want it to decrease as k increases, and i don't know hot to calculate a sum like this.
> >
> > A correction:
> > x8=(x7*e^(-1))/e^(-1)
> > ie without using x1,x2,x3 but only the immediate previous non-zero values
>
> This is what I have so far:
> s=0;
> s1=0;
> i=1;
> while i<=length(x)
> for j=1:length(x)
> if x(i)~=0
> s=s+???? %%%%%the numerator which I still don't know how to calculate
> s1=s1+e^(-j);
> i=i+1;
> else
> x(i)=s/s1;
> i=i+1;
> j=1;
> s=0;
> s1=0;
> end
> end
> end
> This seems to work fine for the denominator. ANy ideas about the numerator?
It doesn't work though if I have subsequent zeros as it devides with zero when I set s1=0 inside the if..I'm at a loss

Subject: sum of exponentials

From: Roger Stafford

Date: 23 Nov, 2012 20:46:08

Message: 7 of 10

"dwi" wrote in message <k8o3vo$ia$1@newscl01ah.mathworks.com>...
> A correction:
> x8=(x7*e^(-1))/e^(-1)
> ie without using x1,x2,x3 but only the immediate previous non-zero values
- - - - - - - - -
  With the correction you made, here is my modified code:

 % An example:
 x = [12,17,21,0,0,0,31,0,0];
 e = exp(1); % <-- I assume this is what 'e' is

 % The code:
 a = 0; b = 1; f = 0;
 for k = 1:length(x)
   if x(k) ~= 0
     a = x(k) + a*f*e^(-1);
     b = 1 + b*f*e^(-1);
     f = 1;
   else
     x(k) = a/b;
     f = 0;
   end
 end

 % The results:

 [(x(3)+x(2)*e^(-1)+x(1)*e^(-2))/(1+e^(-1)+e^(-2));
   x(4);
   x(5);
   x(6)] =

  19.21081095724738
  19.21081095724738
  19.21081095724738
  19.21081095724738

 [x(7);
  x(8);
  x(9)] =

    31
    31
    31

> Thank you for your answer but if I use this the index of the exponent never changes.I want it to decrease as k increases, and i don't know hot to calculate a sum like this.

  In the earlier thread where you said, "but if I use this the index of the exponent never changes", I don't know what you meant, but that code was doing just what you had asked for at that time.

Roger Stafford

Subject: sum of exponentials

From: dwi

Date: 24 Nov, 2012 14:34:07

Message: 8 of 10

"Roger Stafford" wrote in message <k8onag$2kt$1@newscl01ah.mathworks.com>...
> "dwi" wrote in message <k8o3vo$ia$1@newscl01ah.mathworks.com>...
> > A correction:
> > x8=(x7*e^(-1))/e^(-1)
> > ie without using x1,x2,x3 but only the immediate previous non-zero values
> - - - - - - - - -
> With the correction you made, here is my modified code:
>
> % An example:
> x = [12,17,21,0,0,0,31,0,0];
> e = exp(1); % <-- I assume this is what 'e' is
>
> % The code:
> a = 0; b = 1; f = 0;
> for k = 1:length(x)
> if x(k) ~= 0
> a = x(k) + a*f*e^(-1);
> b = 1 + b*f*e^(-1);
> f = 1;
> else
> x(k) = a/b;
> f = 0;
> end
> end
>
> % The results:
>
> [(x(3)+x(2)*e^(-1)+x(1)*e^(-2))/(1+e^(-1)+e^(-2));
> x(4);
> x(5);
> x(6)] =
>
> 19.21081095724738
> 19.21081095724738
> 19.21081095724738
> 19.21081095724738
>
> [x(7);
> x(8);
> x(9)] =
>
> 31
> 31
> 31
>
> > Thank you for your answer but if I use this the index of the exponent never changes.I want it to decrease as k increases, and i don't know hot to calculate a sum like this.
>
> In the earlier thread where you said, "but if I use this the index of the exponent never changes", I don't know what you meant, but that code was doing just what you had asked for at that time.
>
> Roger Stafford

Ok, I understand now how this works. But still, you said the result will be
x(3)+x(2)*e^(-1)+x(1)*e^(-2))/(1+e^(-1)+e^(-2));
while I want
(x(3)*e^(-1)+x(2)*e^(-2)+x(1)*e^(-3))/(e^(-1)+e^(-2)+e^(-3));
Also, how would your code change if I had e^(-1/20), e^(-2/20), e^(-3/20) etc?
Thank you for your time.This has been extremely helpful!

Subject: sum of exponentials

From: Roger Stafford

Date: 24 Nov, 2012 17:28:06

Message: 9 of 10

"dwi" wrote in message <k8qlsv$ktj$1@newscl01ah.mathworks.com>...
> Ok, I understand now how this works. But still, you said the result will be
> x(3)+x(2)*e^(-1)+x(1)*e^(-2))/(1+e^(-1)+e^(-2));
> while I want
> (x(3)*e^(-1)+x(2)*e^(-2)+x(1)*e^(-3))/(e^(-1)+e^(-2)+e^(-3));
> Also, how would your code change if I had e^(-1/20), e^(-2/20), e^(-3/20) etc?
- - - - - - - -
  The two expressions

 (x(3)*e^(-1)+x(2)*e^(-2)+x(1)*e^(-3))/(e^(-1)+e^(-2)+e^(-3))

and

 (x(3)+x(2)*e^(-1)+x(1)*e^(-2))/(1+e^(-1)+e^(-2))


are identically equal. Just divide the numerator and denominator of the first expression by e^(-1) to get the second expression. What you want and what this code produces are the same thing.

  As to your second question, just the two lines

 a = x(k) + a*f*e^(-1);
 b = 1 + b*f*e^(-1);

would need to be changed to:

 a = x(k) + a*f*e^(-1/20);
 b = 1 + b*f*e^(-1/20);

Roger Stafford

Subject: sum of exponentials

From: dwi

Date: 25 Nov, 2012 00:16:08

Message: 10 of 10

"Roger Stafford" wrote in message <k8r036$nn7$1@newscl01ah.mathworks.com>...
> "dwi" wrote in message <k8qlsv$ktj$1@newscl01ah.mathworks.com>...
> > Ok, I understand now how this works. But still, you said the result will be
> > x(3)+x(2)*e^(-1)+x(1)*e^(-2))/(1+e^(-1)+e^(-2));
> > while I want
> > (x(3)*e^(-1)+x(2)*e^(-2)+x(1)*e^(-3))/(e^(-1)+e^(-2)+e^(-3));
> > Also, how would your code change if I had e^(-1/20), e^(-2/20), e^(-3/20) etc?
> - - - - - - - -
> The two expressions
>
> (x(3)*e^(-1)+x(2)*e^(-2)+x(1)*e^(-3))/(e^(-1)+e^(-2)+e^(-3))
>
> and
>
> (x(3)+x(2)*e^(-1)+x(1)*e^(-2))/(1+e^(-1)+e^(-2))
>
>
> are identically equal. Just divide the numerator and denominator of the first expression by e^(-1) to get the second expression. What you want and what this code produces are the same thing.
>
> As to your second question, just the two lines
>
> a = x(k) + a*f*e^(-1);
> b = 1 + b*f*e^(-1);
>
> would need to be changed to:
>
> a = x(k) + a*f*e^(-1/20);
> b = 1 + b*f*e^(-1/20);
>
> Roger Stafford
Ok, thank you!

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us