Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

vector calculations

6 views
Skip to first unread message

davew

unread,
Oct 17, 2009, 11:14:48 AM10/17/09
to
Hi,
I have realised that using for loops is *very* slow in Scilab. I
would like to do the following with vector calculations (which I'm led
to believe is the natural way to do things in Scilab). Could anyone
tell me how to code it?

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.

Jean-Pierre Vial

unread,
Oct 17, 2009, 5:20:01 PM10/17/09
to
davew a �crit :

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

davew

unread,
Oct 17, 2009, 7:58:48 PM10/17/09
to
On 17 Oct, 22:20, Jean-Pierre Vial <vial...@nerim.net> wrote:
> davew a écrit :

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,

Tim Wescott

unread,
Oct 17, 2009, 8:22:42 PM10/17/09
to

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...

--
www.wescottdesign.com

davew

unread,
Oct 19, 2009, 7:02:38 PM10/19/09
to
> --www.wescottdesign.com- Hide quoted text -
>
> - Show quoted text -

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.

0 new messages