x=some vector of size N
y=zeros(x);
for n=2:N,
y(1:n)=y(1:n-1)+x(1:n);
end
I've tried using simple vector statements but I think Scilab takes y
as a vector of zeros but doesn't take into account the fact that it is
updated on every iteration.
Many thanks.
y(1:n)=y(1:n-1)+x(1:n); is meaningless, you add two vectors of different
sizes
maybe you want this:
y=zeros(x);
for n=2:N
y(1:n-1)=y(1:n-1)+x(1:n-1);
y(n)=x(n);
end
but you can get the same result much faster by analizing the operation:
z=zeros(x);
z(2:N)=[N-1:-1:1];
z(1)=N-1;
y=z .* x; DO NOT FORGET the . before the *
or may be I did not understand what you want
I'm sorry, I think I have wasted your time by asking the question
using incorrect syntax for Scilab. May I ask again, but use a C
example this time?
float x [5] = {1,2,3,4,5};
float y [5] = {0,0,0,0,0};
int n;
y [0] = x [0];
for (n=1; n<5; n++)
{
y [n] = y [n-1] + x [n];
}
So, we can't calculate y[n] until we have calculated the previous y
[n-1]. Is this possible without a loop in Scilab?
Many thanks,
That's a difference equation. The _easy_ way to solve that particular
one is to notice that y[n] equals the sum of x from 1 to n, then note
that you have reinvented the Scilab cumsum function:
y = cumsum(x);
If you want to do some _other_ difference equation, then you'd make it
into a z-domain transfer function or a state-space system description,
and you'd use the flts function to find its response.
There's a bazillion different references on going from a difference
equation to a transfer function. Here's one (which really ought to refer
to Scilab...): http://www.wescottdesign.com/articles/zTransform/z-
transforms.html.
Hope this helps...
OK, thanks for that Tim. It's actually a bit more complicated and I
was trying to simplify the example and made a bit of a hash of it! It
is a difference equation as you say but it's not time invariant i.e.
I'm changing the time constant coefficient depending on the actual
data (though my simplified example doesn't show this). I realised
what my main problem was though - my output data was not pre-allocated
using "zeros()", so Scilab was re-allocating my output vector for each
added sample. This caused the whole thing to grind to a halt very
quickly. Now, I have corrected that and it's much faster. I don't
think I can do what I'm doing with vector operations after all.