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

shift vector

528 views
Skip to first unread message

eb...@hotmail.com

unread,
Aug 19, 2005, 12:54:27 PM8/19/05
to
Hello all,
I would like to know how can a shift a vector into Scilab, like we can
do in Octave. For example, let

x = [0 1 2 3 4]

shift(x,1)
4 0 1 2 3

shift(x,-1)
1 2 3 4 0

Using Scilab help I found only function "fftshift".

Thanks in advance!
Eduardo.

Francois Vogel

unread,
Aug 19, 2005, 1:37:38 PM8/19/05
to
I don't think this is readily available in the core, but the following will
do it for positive n (you can esaily adapt for egative n):

function sv = shift(v,n)
if (size(v,'r')<>1 & size(v,'c')<>1) then
error("1st argument must be column vector or row vector")
end
if size(v,'r')<>1 then
v = shift(v',n)
sv = v'
else
n = modulo(n,size(v,'c'))
sv = v($-n+1:$)
sv = [sv v(1:$-n)]
end
endfunction

x = [0 1 2 3 4]

shift(x,6)

y=x'
shift(y,2)


If you need a bulletproof function, you'll have to do a bit more work (test
the type of the input arguments for instance).

HTH.
Francois


<eb...@hotmail.com> a écrit dans le message de news:
1124470466....@z14g2000cwz.googlegroups.com...

eb...@hotmail.com

unread,
Aug 22, 2005, 12:56:12 PM8/22/05
to
Dear Francois,
during the weekend I implemented two new functions into Scilab and
would like to share with you and newsgroup (listed below).

* First function (DERIV.SCI) was adapted from IDL software to performs
numerical differentiation using 3-point derivation and Lagrangian
interpolation.

* The other (SHIFT.SCI) was adapeted from Octave and just shifts
elements of vectors or arrays along any dimension.

I will share these functions in Scilab website.

Thanks for attention!
Eduardo.

==> Functions <==

// The DERIV function performs numerical differentiation using 3-point,
Lagrangian interpolation and returns the derivative.
//
// Syntax
//
// Result = DERIV([X,] Y)
//
// Arguments
// X
//
// Differentiate with respect to this variable. If omitted, unit
spacing for Y (i.e., Xi = i) is assumed.
//
// Y
//
// The variable to be differentiated.
//
// Scilab example:
//
// -->X = [ 0.1, 0.3, 0.4, 0.7, 0.9]
// X =
//
// ! 0.1 0.3 0.4 0.7 0.9 !
//
// -->Y = [ 1.2, 2.3, 3.2, 4.4, 6.6]
// Y =
//
// ! 1.2 2.3 3.2 4.4 6.6 !
//
// -->DERIV(Y)
// ans =
//
// ! 1.2 1. 1.05 1.7 2.7 !
//
// -->DERIV(X,Y)
// ans =
//
// ! 8. 6.6666667 5.25 6.8 10.8 !
//

// Author: Eduardo Barbosa <eb...@hotmail.com>
// Created: 20 August 2005
// Adapted-From: IDL Software

function d = DERIV(varargin)
x = varargin(1);
n = length(x);

if n < 3 then
printf("Parameters must have at least 3 points");
return(-1);
end

if length(varargin) >= 2 then
y = varargin(2);
if n <> length(y) then
printf("Vectors must have same size");
return(-1);
end

dy = (SHIFT(y,-1) - SHIFT(y,1));
dx = (SHIFT(x,-1) - SHIFT(x,1));
d = dy./dx;
d(1) = (-3.*y(1) + 4.*y(2) - y(3))/(x(3)-x(1));
d(n) = (3.*y(n) - 4.*y(n-1) + y(n-2))/(x(n)-x(n-2));
else
d = (SHIFT(x,-1) - SHIFT(x,1))/2.;
d(1) = (-3.*x(1) + 4.*x(2) - x(3))/2.;
d(n) = (3.*x(n) - 4.*x(n-1) + x(n-2))/2.;
end
endfunction

// The SHIFT function shifts elements of vectors or arrays along any
dimension by any number of elements. The result is a vector or array of
the same structure and type as Array. Positive shifts are to the right
while left shifts are expressed as a negative number. All shifts are
circular.
// Elements shifted off one end wrap around and are shifted onto the
other end. In the case of vectors the action of SHIFT can be expressed:
//
// Result(i + s) modulation = Arrayi for (0 £ 1 < n)
//
// where s is the amount of the shift, and n is the number of elements
in the array.
//
// Syntax
//
// Result = SHIFT(Array, S1, ..., Sn)
//
// Arguments
// Array
//
// The array to be shifted.
//
// Si
//
// The shift parameters. For arrays of more than one dimension, the
parameter Sn specifies the shift applied to the nth dimension. S1
specifies the shift along the first dimension and so on. If only one
shift parameter is present and the parameter is an array, the array is
treated as a vector (i.e., the array is treated as having
one-dimensional subscripts).
//
// A shift specification of 0 means that no shift is to be performed
along that dimension.
//
// Scilab example:
//
// -->A = [1.2, 2.3, 3.2, 4.4, 6.6] // the original vector
// A =
//
// ! 1.2 2.3 3.2 4.4 6.6 !
//
// -->SHIFT(A,1) // the vector shifted one position to the right
// ans =
//
// ! 6.6 1.2 2.3 3.2 4.4 !
//
// -->SHIFT(A,-1) // vector shifted one position to the left
// ans =
//
// ! 2.3 3.2 4.4 6.6 1.2 !
//
// Notice how elements of the vector that shift off the end wrap around
to the other end. This "wrap around" occurs when shifting arrays of any
dimension.
//

// Author: Eduardo Barbosa <eb...@hotmail.com>
// Created: 20 August 2005
// Adapted-From: Octave Software

function [y] = SHIFT(varargin)
if length(varargin) <> 2 then
printf("usage: shift (X, b)");
return(-1);
end

x = varargin(1);
b = varargin(2);

[nr,nc] = size(x);

if nr == 0 | nc == 0 then
error ("shift: x must not be empty");
else
if nr == 1 then
x = x.';
nr = nc;
nc = 0;
end
end

[dr,dc] = size(b);

if dr > 1 | dc > 1 | isreal(b) == "F" then
printf("shift: b must be an integer");
return(-1);
end

if b >= 0 then
b = modulo(b,nr);
t1 = x(nr-b+1:nr, :);
t2 = x(1:nr-b, :);
y = [t1; t2];
else
if b < 0 then
b = modulo(abs(b),nr);
t1 = x(b+1:nr,:);
t2 = x(1:b,:);
y = [t1;t2];
end
end

y = y.';
endfunction

Francois Vogel

unread,
Aug 22, 2005, 2:53:21 PM8/22/05
to
Is Result = SHIFT(Array, S1, ..., Sn) really an allowed input?


-->A=eye(3,3)
A =

! 1. 0. 0. !
! 0. 1. 0. !
! 0. 0. 1. !

-->SHIFT(A,2,0)
usage: shift (X, b) !--error 4
undefined variable : y
at line 4 of function SHIFT called by :
SHIFT(A,2,0)


ebmb

unread,
Aug 22, 2005, 4:08:19 PM8/22/05
to
Francois,
sorry, but that input is not allowed, there an error in sintax... To
get correct results, try this:

Result = SHIFT(Array, S), it performs a circular shift of length of S.

The correct sintax is

// Syntax
//
// Result = SHIFT(Array, S)
//

Thanks for comments,
Eduardo.

Francois Vogel

unread,
Aug 22, 2005, 4:18:08 PM8/22/05
to
I was referring to the comment at the beginning of your script:

<quote>
// Syntax
//


// Result = SHIFT(Array, S1, ..., Sn)
//
// Arguments
// Array
//
// The array to be shifted.
//
// Si
//
// The shift parameters. For arrays of more than one dimension, the
parameter Sn specifies the shift applied to the nth dimension. S1
specifies the shift along the first dimension and so on. If only one
shift parameter is present and the parameter is an array, the array is
treated as a vector (i.e., the array is treated as having
one-dimensional subscripts).

<\quote>

Unless I misunderstand something, this really lets you think there can be
more than one S parameter, thus performing a shift to more than one
dimension of a multidimensional array (which puts in the problem of the
order with which the shifts are applied to dimensions).

Francois

"ebmb" <eb...@hotmail.com> a écrit dans le message de news:
1124741299.5...@g14g2000cwa.googlegroups.com...

ebmb

unread,
Aug 22, 2005, 5:02:46 PM8/22/05
to
Francois,
sorry, but that input is not allowed, there an error in sintax... To
get correct results, try this:

Result = SHIFT(Array, S), it performs a circular shift of length of S.

The correct sintax is

// Syntax
//

bruno

unread,
Aug 23, 2005, 4:49:00 AM8/23/05
to

Hi,

just a remark about deriv : you can have the same
effect (in one line) with the function splin because
its output is the vector of the derivatives at the
breakpoints.

(moreover with the option fast you may have the same
result than with your deriv).

d = splin(x,y)
d = splin(x,y,"fast")

hth
Bruno

ebmb

unread,
Aug 23, 2005, 12:24:47 PM8/23/05
to
Bruno,
I didn't know this function.
Thanks for suggestion,
Eduardo.

ebmb

unread,
Aug 23, 2005, 12:30:58 PM8/23/05
to
Francois,
the comments and sintax at the beginning of script are really wrong. I
will erase them...

Thanks for your attention and observations,
Eduardo.

0 new messages