[julia-users] Re: Find the indice of the element with the nearest value of a float in an array

2016-04-11 Thread DNF
That's right.  '[]' for collecting should be replaced with 'collect'. But 
in your case you shouldn't have used '[]' in the first place :)

It seems that it is very common for people to collect ranges into arrays, 
but it is rare that you actually need to. The best approach is to *not* 
collect, 
and then see if it works the way you intend it to. 


On Monday, April 11, 2016 at 12:15:31 PM UTC+2, Fred wrote:
>
> Thank you for this explanation DNF ! It is the first time I use collect and 
> the reason why I used it was an error message suggesting that I should use 
> it :) maybe because of my first mistake to use []. 
>
> Le dimanche 10 avril 2016 23:30:10 UTC+2, DNF a écrit :
>>
>> The big error you made originally is calling collect in every iteration 
>> of the loop. Just deleting collect speeds things up by 100x. The lesson 
>> is that you should (almost) never use collect.
>>
>> The other lesson is: don't do [1:0.1:10]. It makes your code slower, and 
>> very soon your it will stop working correctly. Just delete [].
>>
>

[julia-users] Re: Find the indice of the element with the nearest value of a float in an array

2016-04-11 Thread Fred
Thank you for this explanation DNF ! It is the first time I use collect and 
the reason why I used it was an error message suggesting that I should use 
it :) maybe because of my first mistake to use []. 

Le dimanche 10 avril 2016 23:30:10 UTC+2, DNF a écrit :
>
> The big error you made originally is calling collect in every iteration 
> of the loop. Just deleting collect speeds things up by 100x. The lesson 
> is that you should (almost) never use collect.
>
> The other lesson is: don't do [1:0.1:10]. It makes your code slower, and 
> very soon your it will stop working correctly. Just delete [].
>


[julia-users] Re: Find the indice of the element with the nearest value of a float in an array

2016-04-10 Thread Cedric St-Jean
Am I alone in pining for a predicate version of findmin/findmax et al., or 
a `by` keyword argument? It would make this code much simpler. 

On Sunday, April 10, 2016 at 5:34:25 PM UTC-4, DNF wrote:
>
>
> I messed up copy-paste here:
>
> >> test_dist(1:0.1:10, 8.22, 1)
>> 0.641741 seconds (1.82 M allocations: 749.817 MB, 7.57% gc time)
>> 0.007380 seconds
>> 0.005570 seconds
>>
>>
> It should be (looping 1 times)
> >> test_dist(1:0.1:10, 8.22, 10^4) 
> 0.641741 seconds (1.82 M allocations: 749.817 MB, 7.57% gc time)
> 0.007380 seconds
> 0.005570 seconds
>
>

[julia-users] Re: Find the indice of the element with the nearest value of a float in an array

2016-04-10 Thread DNF

I messed up copy-paste here:

>> test_dist(1:0.1:10, 8.22, 1)
> 0.641741 seconds (1.82 M allocations: 749.817 MB, 7.57% gc time)
> 0.007380 seconds
> 0.005570 seconds
>
>
It should be (looping 1 times)
>> test_dist(1:0.1:10, 8.22, 10^4) 
0.641741 seconds (1.82 M allocations: 749.817 MB, 7.57% gc time)
0.007380 seconds
0.005570 seconds



[julia-users] Re: Find the indice of the element with the nearest value of a float in an array

2016-04-10 Thread DNF
The big error you made originally is calling collect in every iteration of 
the loop. Just deleting collect speeds things up by 100x. The lesson is 
that you should (almost) never use collect.

The other lesson is: don't do [1:0.1:10]. It makes your code slower, and 
very soon your it will stop working correctly. Just delete [].

function mindist0(x, val)
# your original
min_i = 0
min_x = 1.0
for i=1:length(x)
e = abs(collect(x)[i] - val)
if e < min_x
min_x = e
min_i = i
end
end
return (min_i, min_x)
end


function mindist1(x, val)
#  same as the original, except removing 'collect'
min_i = 0
min_x = 1.0
for i = 1:length(x)
e = abs(x[i] - val)
if e < min_x
min_x = e
min_i = i
end
end
return (min_i, min_x)
end


function mindist2(x, val)
# using enumerate to avoid indexing
min_i = 0
min_x = Inf
for (i, xi) in enumerate(x)
dist = abs(xi - val)
if dist < min_x
min_x = dist
min_i = i
end
end
return (min_i, min_x)
end


function test_dist(x, val, N)
# putting time testing inside a function for optimal result
@assert mindist0(x, val) == mindist1(x, val) == mindist2(x, val)
@time for _ in 1:N mindist0(x, val) end
@time for _ in 1:N mindist1(x, val) end
@time for _ in 1:N mindist2(x, val) end
end


>> test_dist(1:0.1:10, 8.22, 1)
0.60 seconds (182 allocations: 76.781 KB)
0.01 seconds
0.01 seconds

>> test_dist(1:0.1:10, 8.22, 1)
0.641741 seconds (1.82 M allocations: 749.817 MB, 7.57% gc time)
0.007380 seconds
0.005570 seconds



[julia-users] Re: Find the indice of the element with the nearest value of a float in an array

2016-04-10 Thread Ravi S
Not sure which is faster, but here's another way to do it:

julia> 
reduce((x,y)->x[2](x,abs(y-8.22)),1:length(x),x))
(73,0.02135)

- Ravi

On Sunday, April 10, 2016 at 5:10:07 PM UTC+5:30, Fred wrote:
>
> Hi,
>
> I am looking for the most efficient (fastest) way to find the indice of 
> the element with the nearest value of a float in an array.
>
> x = [1:0.1:10]
>
> julia> x
> 91-element Array{Float64,1}:
>   1.0
>   1.1
>   1.2
>   1.3
>   1.4
>   ⋮  
>   9.4
>   9.5
>   9.6
>   9.7
>   9.8
>   9.9
>  10.0
>
> It is very easy to find the indice of an exact value of x, for example 8.2
>
> julia> find(x .== 8.2)
> 1-element Array{Int64,1}:
>  73
>
> But if I want the indice of the closest value of 8.22
>
> julia> minimum(abs(x-8.22))
> 0.02135
>
> julia> find(x .== minimum(abs(x-8.22)))
> 0-element Array{Int64,1}
>
>
> Of course it is easy to do that with a loop but is it the fastest 
> solution ?
>
> min_i = 0
> min_x = 1.0
>
>  for i=[1:length(x)]
>e = abs(collect(x)[i] - 8.22)
>if e < min_x
>min_x = e
>min_i = i
>end
>  end
>
> println(min_x, " -> ", min_i)
> 0.02135 -> 73
>
>
> Thanks for your comments !
>
>

[julia-users] Re: Find

2015-11-14 Thread Steven G. Johnson


On Saturday, November 14, 2015 at 9:56:21 AM UTC-5, digxx wrote:
>
> Sorry I already deleted it since find(x.>3) just works...
> Though in the manual it says its a 2 argument function
> first argument the condition and second the array/vector... 
>

The manual documents both the 2-argument and 1-argument versions.


[julia-users] Re: Find

2015-11-14 Thread digxx
Sorry I already deleted it since find(x.>3) just works...
Though in the manual it says its a 2 argument function
first argument the condition and second the array/vector...

Am Samstag, 14. November 2015 15:54:10 UTC+1 schrieb Dan:

> `x .> 3` is a bit-array. The function form, which is probably what fits, 
> is `x->x.>3` (the back-ticks are for quotation, not part of the code).   



[julia-users] Re: Find linearly independent subset within a set of vectors without constructing a matrix

2015-10-09 Thread Matt
I should have precised that n << length(v)

A good way in this case is to form the cross product matrix (by a 
combination of dot(slice, slice)), and call cholfact!(x, :U, Val{true}) on 
it

On Monday, October 5, 2015 at 11:31:24 AM UTC-4, Glen O wrote:
>
> I'm assuming that v is an array of vectors. Does this do any better?
>
> N=zeros(length(v[1]),max(length(v[1]),length(v))) # Because there can't be 
> more than this many columns
> i=0
> for w=v
> if any(w.!=0)&&w!=N[:,1:i]*(N[:,1:i]\w)
>   i+=1
>   N[:,i]=j
> end
> end
> N=N[:,1:i]
>
> Note: may have minor issue for rounding errors, so some rounding during 
> testing may be necessary to avoid problems. Also, this is written from 0.3, 
> I cannot guarantee that everything will work correctly in 0.4.
>
> On Tuesday, 6 October 2015 01:06:15 UTC+10, Matt wrote:
>>
>> Given a set of vectors v1, , vn, I'd like to construct a matrix where 
>> columns correspond to a linearly independent subset of these vectors.
>> Currenly, I form the matrix hcat(v1, ..., vn), use qrfact!() on it, check 
>> the diagonal of the :R matrix and construct a new matrix as hcat(vj,...)
>> I'm looking for a way that would use less memory. Is there a more memory 
>> efficient way ? (for instance a way to avoid the construction of an 
>> intermediary matrix when finding the subset of vectors, or a way to delete 
>> columns in place in a matrix)
>>
>

[julia-users] Re: Find linearly independent subset within a set of vectors without constructing a matrix

2015-10-05 Thread Glen O
I'm assuming that v is an array of vectors. Does this do any better?

N=zeros(length(v[1]),max(length(v[1]),length(v))) # Because there can't be 
more than this many columns
i=0
for w=v
if any(w.!=0)&&w!=N[:,1:i]*(N[:,1:i]\w)
  i+=1
  N[:,i]=j
end
end
N=N[:,1:i]

Note: may have minor issue for rounding errors, so some rounding during 
testing may be necessary to avoid problems. Also, this is written from 0.3, 
I cannot guarantee that everything will work correctly in 0.4.

On Tuesday, 6 October 2015 01:06:15 UTC+10, Matt wrote:
>
> Given a set of vectors v1, , vn, I'd like to construct a matrix where 
> columns correspond to a linearly independent subset of these vectors.
> Currenly, I form the matrix hcat(v1, ..., vn), use qrfact!() on it, check 
> the diagonal of the :R matrix and construct a new matrix as hcat(vj,...)
> I'm looking for a way that would use less memory. Is there a more memory 
> efficient way ? (for instance a way to avoid the construction of an 
> intermediary matrix when finding the subset of vectors, or a way to delete 
> columns in place in a matrix)
>


Re: [julia-users] Re: find nearest non-zero element in matrix

2014-08-12 Thread Stefan Karpinski
If it needs to be fast, I would just write the logic out with for loops.


On Tue, Aug 12, 2014 at 10:59 AM, tcs  wrote:

> In other words, is there anything more optimized for this problem than
> concatenating findn and the distance measure of which I pick the minimum?
>
>
> On Tuesday, August 12, 2014 10:24:47 AM UTC-4, tcs wrote:
>>
>> Hi julia-users,
>>
>> I am looking for a function that takes a two-dimensional matrix and a set
>> of matrix indices as inputs and spits out the nearest non-zero element in
>> taxicab distance over the matrix index.
>> I am asking for your advice because this function needs to be very fast
>> as I have to do this search many times. Any ideas or advice would be
>> greatly appreciated.
>>
>> Thanks!
>>
>


[julia-users] Re: find nearest non-zero element in matrix

2014-08-12 Thread tcs
In other words, is there anything more optimized for this problem than 
concatenating findn and the distance measure of which I pick the minimum?

On Tuesday, August 12, 2014 10:24:47 AM UTC-4, tcs wrote:
>
> Hi julia-users,
>
> I am looking for a function that takes a two-dimensional matrix and a set 
> of matrix indices as inputs and spits out the nearest non-zero element in 
> taxicab distance over the matrix index. 
> I am asking for your advice because this function needs to be very fast as 
> I have to do this search many times. Any ideas or advice would be greatly 
> appreciated.
>
> Thanks!
>


[julia-users] Re: find if a pointer is NULL

2014-06-15 Thread Eric Davies
It seems like the method is gone. It exists for Uint64 integers but no 
others. You can get the behaviour you want using convert(Ptr{*type*}, 
*integer*). It should probably be added back, either calling the existing 
pointer method or by calling convert. 

On Saturday, 14 June 2014 21:39:24 UTC-5, J Luis wrote:
>
> How do I test if a pointer is NULL?
> (did search the docs but couldn't find and answer)
>
> and BTW, I can't get this one either
>
> docs:
>
> pointer(*type*, *int*)
>
> Convert an integer to a pointer of the specified element type.
>
> julia> pointer(Uint8,0)
> ERROR: no method pointer(Type{Uint8}, Int64)
>
> julia> pointer(Uint8,uint8(0))
> ERROR: no method pointer(Type{Uint8}, Uint8)
>
> Thanks
>