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

Samuel Gougeon sgougeon at free.fr
Wed Dec 5 22:12:47 CET 2018


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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20181205/c3bd1c0e/attachment.htm>


More information about the users mailing list