[Scilab-users] Inserting %nan at given positions into a vector

classic Classic list List threaded Threaded
3 messages Options
Jens Jens
Reply | Threaded
Open this post in threaded view
|

[Scilab-users] Inserting %nan at given positions into a vector

Hallo Scilab experts,
Given is a numeric vector x of length n and an index vector i with  max(i)=n. I want to insert a %nan after every x(i).

Example
x=[11:20]
i=[2 5 8]
Required result: y=[11 12 %nan 13 14 15 %nan 16 17 18 %nan 19 20]

How would you proceed so that it works fast for n>500000 too.

Kind regards

Jens



_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Samuel GOUGEON Samuel GOUGEON
Reply | Threaded
Open this post in threaded view
|

Re: Inserting %nan at given positions into a vector

Le 05/12/2018 à 13:01, Jens Simon Strom a écrit :
Hallo Scilab experts,
Given is a numeric vector x of length n and an index vector i with  max(i)=n. I want to insert a %nan after every x(i).

Example
x=[11:20]
i=[2 5 8]
Required result: y=[11 12 %nan 13 14 15 %nan 16 17 18 %nan 19 20]

How would you proceed so that it works fast for n>500000 too.

The following code does it:

x = 11:20;
i = [2 5 8];
//                   [11 12      13 14 15      16 17 18      19 20]
//Required result: y=[11 12 %nan 13 14 15 %nan 16 17 18 %nan 19 20]
nx = size(x,"*");
ni = size(i,"*");
o = ones(1,nx);
o(i+1) = 2;
newx = zeros(1,nx+ni);
newx(cumsum(o)) = x;
newx(i+(1:ni)) = %nan

--> newx(i+(1:ni)) = %nan
 newx  = 
   11.   12.   Nan   13.   14.   15.   Nan   16.   17.   18.   Nan   19.   20.

Regards
Samuel


_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Jens Jens
Reply | Threaded
Open this post in threaded view
|

Re: Inserting %nan at given positions into a vector

Hallo Samuel,
Tanquam ex ungue leonem! Great!

Kind regards
Jens
-------------------------------------------------------------


Am 05.12.2018 22:12, schrieb Samuel Gougeon:
Le 05/12/2018 à 13:01, Jens Simon Strom a écrit :
Hallo Scilab experts,
Given is a numeric vector x of length n and an index vector i with  max(i)=n. I want to insert a %nan after every x(i).

Example
x=[11:20]
i=[2 5 8]
Required result: y=[11 12 %nan 13 14 15 %nan 16 17 18 %nan 19 20]

How would you proceed so that it works fast for n>500000 too.

The following code does it:

x = 11:20;
i = [2 5 8];
//                   [11 12      13 14 15      16 17 18      19 20]
//Required result: y=[11 12 %nan 13 14 15 %nan 16 17 18 %nan 19 20]
nx = size(x,"*");
ni = size(i,"*");
o = ones(1,nx);
o(i+1) = 2;
newx = zeros(1,nx+ni);
newx(cumsum(o)) = x;
newx(i+(1:ni)) = %nan

--> newx(i+(1:ni)) = %nan
 newx  = 
   11.   12.   Nan   13.   14.   15.   Nan   16.   17.   18.   Nan   19.   20.

Regards
Samuel



_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users


_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users