The reason why it's not working is that you're re-assigning x, rather than 
editing it. By running "x=x[....]", you're creating a copy of x with the 
appropriate column removed, and then assigning x to point to the copy, 
rather than to the original array.

If you wanted to edit an array's values, you'd do something like 
"x[:]=x[end:-1:1]" to reverse them in-place - this creates a reversed copy, 
and then copies the values from that reversed copy into the original array. 
Unfortunately, for removing columns or values, it's not quite so simple, 
because you have to deal with changing the size of the matrix.

To get a feel for what is involved, have a look at this:

https://github.com/JuliaLang/julia/blob/master/base/array.jl

That file has the definitions of the functions that you're trying to mimic. 
As you'll see, they end up using "ccall" functions, which are used to call 
C and Fortran libraries. I believe these are necessary if you wish to alter 
the dimensions of an array in-place.

On Thursday, 22 October 2015 09:55:55 UTC+10, Thuener Silva wrote:
>
> I want to change a matrix inside a function. Example( just to illustrate):
>
> julia> x = ones(2,3)
> 2x3 Array{Float64,2}:
>  1.0  1.0  1.0
>  1.0  1.0  1.0
>
> julia> function deletecolumns(x,i)
>           x = x[:,1:size(x,2) .!= i]
>        end
> deletecolumns (generic function with 1 method)
>
> julia> deletecolumns(x,1)
> 2x2 Array{Float64,2}:
>  1.0  1.0
>  1.0  1.0                                                                 
>                                                             
>                                                                           
>                                                             
> julia> x
> 2x3 Array{Float64,2}:                                                     
>                                                             
>  1.0  1.0  1.0                                                             
>                                                            
>  1.0  1.0  1.0  
>
> The best way is to do "x = deletecolumns(x,1)" ? I want to make something 
> more like delete!(x,1).
>
>
> Thanks in advance,
> Thuener Silva
>

Reply via email to