exercise on list
philippe
rouxph.22 at gmail.com
Sat Apr 14 10:55:34 CEST 2012
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.
More information about the users
mailing list