[Scilab-users] Loop query
Stefan Du Rietz
sdr at durietz.se
Sun Aug 15 16:06:46 CEST 2021
One solution:
// Run the last (largest):
[ns, seq] = collatz(prime($))
// insert in new zeros vector:
ns = [zeros(prime-1), ns]
// insert seq in new zeros matrix:
seq = [zeros(length(seq), length(ns)-1), seq];
for i = 1:length(prime)-1
// Run collatz to get the new ns:
[nsi, seqi] = collatz(prime(i));
// insert in vector ns and matrix seq:
ns(i) = nsi;
seq(1:nsi, i) = seqi;
end
dispalay ns above seq:
[ns; seq]
Stefan
On 2021-08-15 13:03, Lester Anderson wrote:
> Hi Stefan,
>
> Thank you for clarifying the meaning of the error message.
>
> Lester
>
> On Sun, 15 Aug 2021 at 11:59, Stefan Du Rietz <sdr at durietz.se
> <mailto:sdr at durietz.se>> wrote:
>
> Hello Lester,
> the problem is that seq in each loop is a vector of increasing length!
>
> Stefan
>
>
> On 2021-08-15 12:05, Lester Anderson wrote:
> > Hi Stefan,
> >
> > I did try that before, but got an error - "Submatrix incorrectly
> defined"
> >
> > Lester
> >
> > On Sun, 15 Aug 2021 at 10:56, Stefan Du Rietz <sdr at durietz.se
> <mailto:sdr at durietz.se>
> > <mailto:sdr at durietz.se <mailto:sdr at durietz.se>>> wrote:
> >
> >
> >
> > On 2021-08-15 09:00, Lester Anderson wrote:
> > > Hello,
> > >
> > > Basic query. I have a simple code that applies the Collatz
> > conjecture
> > > equation (3n+1) by running a function and then runs a loop
> over the
> > > values stored in prime (the first 8 Prime numbers):
> > >
> > > clear
> > >
> > > exec('collatz.sci',-1);
> > >
> > > prime = primes(20);
> > >
> > > for i = 1:length(prime)
> > > [ns, seq]=collatz(prime(i))
> > > end
> > >
> > > As it stands, this just runs to the end (i=8) and the
> value 19.
> > How can
> > > I get the code to write the results of each loop pass into the
> > variables
> > > ns and seq, such that each contains the results of the 8
> passes?
> > >
> > > Can no longer search the forums online from the website.
> > >
> > > Thanks
> > > Lester
> >
> >
> > // Before the for loop:
> > ns = zeros(prime);
> > seq = ns;
> >
> > // Changed for loop
> > for i = 1:length(prime)
> > [ns(i), seq(i)]=collatz(prime(i))
> > end
> >
> > Regards
> > Stefan
> > _______________________________________________
> > users mailing list
> > users at lists.scilab.org <mailto:users at lists.scilab.org>
> <mailto:users at lists.scilab.org <mailto:users at lists.scilab.org>>
> > http://lists.scilab.org/mailman/listinfo/users
> <http://lists.scilab.org/mailman/listinfo/users>
> > <http://lists.scilab.org/mailman/listinfo/users
> <http://lists.scilab.org/mailman/listinfo/users>>
> >
> >
> > _______________________________________________
> > users mailing list
> > users at lists.scilab.org <mailto:users at lists.scilab.org>
> > http://lists.scilab.org/mailman/listinfo/users
> <http://lists.scilab.org/mailman/listinfo/users>
> >
> _______________________________________________
> users mailing list
> users at lists.scilab.org <mailto:users at lists.scilab.org>
> http://lists.scilab.org/mailman/listinfo/users
> <http://lists.scilab.org/mailman/listinfo/users>
>
>
> _______________________________________________
> users mailing list
> users at lists.scilab.org
> http://lists.scilab.org/mailman/listinfo/users
>
More information about the users
mailing list