[scilab-Users] Re: exercise on list
Calixte DENIZET
calixte.denizet at scilab-enterprises.com
Sat Apr 14 13:00:18 CEST 2012
Hi,
Funny exercise... :)
i) N=9;L=zeros(1,N*(N+1)/2 - 1);L(cumsum(1:(N - 1)))=1;L=[1 cumsum(L) + 1]
The first idea is to use a diff on the result to observe that there is
ones at position 1,3,6,10,.... and to "revert" a diff use a cumsum.
ii) N=9;L=zeros(1,(N+1)*(N+2)/2 - 1);L(cumsum(1:N))=1:N
The idea is to observe that you've a number at position 1,3,6,10,... so
just put this number at the right position.
Calixte
On 14/04/2012 10:55, philippe wrote:
> Le 08/04/2012 21:04, abd_bela a écrit :
>>
>>
>> without using (loops: for or while) create the list of numbers as
>> follow:
>
> whitout loops it's fun :-)
>
>> 1, 2, 2, 3 3 3 4 4 4 4 5 5 5 5 5 ...... 9 9 9 9 9 9 9 9 9
>
> easy : build a 9x9 matrix with A(i,j)=i, take the lower triangular
> part of A with tril), rewrite it as a one line list (with :), and keep
> only non zero coefficients :
>
> n=9;
> A=(tril(([1:n]')*ones(1:n)))'; B=A(:); C=B(find(B<>0))'
>
>>
>> 1 0 2 0 0 3 0 0 0 4 0 0 0 0 ..... 9 0 0 0 0 0 0 0 0 0
>
> harder, but based on the same ideas :
>
> n=9;A=-ones(n+1,n+1); A(1,2:n+1)=[1:n];
> A=triu(A);B=A(:);C=B(find(B<>0))'; C(find(C==-1))=0; C(1)=[]
>
>
> Philippe.
>
>
> --
> To unsubscribe from this mailing-list, please send an empty mail to
> users-unsubscribe at lists.scilab.org
> To check the archives of this mailing list, see
> http://mailinglists.scilab.org/
>
More information about the users
mailing list