[scilab-Users] Bug or restriction with matrices?

Samuel GOUGEON Samuel.Gougeon at univ-lemans.fr
Fri Jun 5 12:04:26 CEST 2009


----- Message d'origine -----
De : Lucio Agostinho Rocha
Date : 05/06/2009 04:35:
> Hi,
>
> I'm working with matrices and I observed this strange behavior. I'd like to acquire as result a matrix with ten lines and ten columns. Then, I tried two things:
>
> //---Start program
> clear;
> m=zeros(10,10);
> function [] = test()
>     
>   test1(m);
>   
>   m(5,1)=1;
>   disp(m);
> endfunction
>
> function [] = test1(m)
>   
>   m(5,1)=1;
>   disp(m);
>   
> endfunction
>
> //---End program
>
> When I use 'test1' function I acquire the expected result, but when I use only 'test' function, considering 'm' as global, the attribution 'cut' my matrix. Why this occurs?
>
> Thanks in advance,
>
> Lucio
>   
Hello Lucio,

The "m" in the test1(m) prototype is a local copy of the expected argument.
Then, when you run test1() without argument, no m matrix is copied
(and a bit surprisingly, no error occurs). So, the m(5,1)=1 statement
creates the m from scratch, as a minimal object, here a column of 5
lines with 1 at the fifth one, and by default 0 on previous lines.
In the test() definition, writing m(1,4)=1  will then display the  0 0 0 1
line-vector instead.

It is the same in the test() definition: The test1(m) statement does not
modify m (that is a copy of the one from the calling level), since test1()
has no output argument. It just displays it.
The following m(5,1)=1; statement modifies the local copy of m,
that you display just after that, with the expected output.
The m matrix of the calling level is not modified (always only zeros).

HTH
Regards
Samuel




More information about the users mailing list