[Scilab-users] Inserting %nan at given positions into a vector
Jens Simon Strom
j.s.strom at hslmg.de
Thu Dec 6 14:54:03 CET 2018
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
> users at lists.scilab.org
> http://lists.scilab.org/mailman/listinfo/users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20181206/dc728bf9/attachment.htm>
More information about the users
mailing list