On Fri, 2016-04-01 at 11:07, Anonymous <[email protected]> wrote:
> but what if I need to access the coordinates of the nonzero entries of a
> sparse matrix for some reason?  Is there a faster way to do it than using
> my colvals function?

help?> nzrange
search: nzrange

  nzrange(A, col)

  Return the range of indices to the structural nonzero values of a sparse 
matrix column. In conjunction with nonzeros(A) and rowvals(A), this allows for 
convenient
  iterating over a sparse matrix :

  A = sparse(I,J,V)
  rows = rowvals(A)
  vals = nonzeros(A)
  m, n = size(A)
  for i = 1:n
     for j in nzrange(A, i)
        row = rows[j]
        val = vals[j]
        # perform sparse wizardry...
     end
  end

So this gives all the indices of the non-zeros:

A = sparse(I,J,V)
  rows = rowvals(A)
  m, n = size(A)
  for i = 1:n
     col_ind = i
     for j in nzrange(A, i)
        row_ind = rows[j]
        # perform sparse wizardry...
     end
  end


> On Friday, April 1, 2016 at 2:03:31 AM UTC-7, Mauro wrote:
>>
>> The reason rowvals exists is to access the vector of row-indices of a
>> CSC-matrix, i.e. one of the internals of CSC, to allow efficient
>> iteration over the non-zeros.  However, there is no equivalent colvals
>> internal, so there is little reason to do this and even less reason to
>> encourage it.  Consider:
>>
>> julia> s = sprand(10^7, 10^7, 1e-7);
>>
>> julia> @time rowvals(s);
>>   0.000004 seconds (4 allocations: 160 bytes)
>>
>> julia> @time colvals(s);
>>   0.508060 seconds (20 allocations: 533.748 MB, 14.61% gc time)
>>
>>
>> On Fri, 2016-04-01 at 10:48, Anonymous <[email protected] <javascript:>>
>> wrote:
>> > There is a rowals function, and then there is a find function, and the
>> find
>> > function actually allows you to write a one line colvals function:
>> >
>> > colvals(S::SparseMatrixCSC) = round(Int, floor(find(S)/(size(S,
>> 1)+0.1))+1)
>> >
>> > shouldn't someone add this to base?
>>

Reply via email to