[Scilab-users] Loop query

Lester Anderson arctica1963 at gmail.com
Mon Aug 16 11:49:04 CEST 2021


Hi Samuel,

Further to your method, is there a way to vectorise the list output based
on the indices?

One can access the elements as seq(1), seq(5) etc. I did look at
list2vec(seq), but as documented this creates a single column vector.

Thanks

Lester

On Sun, 15 Aug 2021 at 14:55, Samuel Gougeon <sgougeon at free.fr> wrote:

> Le 15/08/2021 à 11:28, Lester Anderson a écrit :
>
> Hello Samuel,
>
> The size of ns (number of steps) and seq (sequence of values) are variable
> depending on the integer input, and this seems to be one issue.
>
> For this reason, seq must be a list, leading to
>
> function [ns, seq] = collatz(p)
>     seq = p
>     while %T
>         if pmodulo(p, 2)
>             p = p*3+1
>         else
>             p = p/2
>         end
>         seq = [seq p]
>         if p==1
>             ns = length(seq)
>             break
>         end
>     endendfunction
> prime = primes(20);[ns, seq] = ([], list());
> for i = 1:length(prime)
>     [ns(i), seq(i)] = collatz(prime(i));end
> --> ns'
>  ans  =
>
>    2.   8.   6.   17.   15.   10.   13.   21.
>
> --> seq
>  seq  =
>
>   (1) = [2,1]
>   (2) = [3,10,5,16,8,4,2,1]
>   (3) = [5,16,8,4,2,1]
>   (4) = [7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1]
>   (5) = [11,34,17,52,26,13,40,20,10,5,16,8,4,2,1]
>   (6) = [13,40,20,10,5,16,8,4,2,1]
>   (7) = [17,52,26,13,40,20,10,5,16,8,4,2,1]
>   (8) = [19,58,29,88,44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1]
>
>
> 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/20210816/d8ce1626/attachment.htm>


More information about the users mailing list