Slicing in sparse

Kim Hansen kimhanse at gmail.com
Tue Dec 4 12:31:35 CST 2007


On Dec 4, 2007 4:46 PM, John W. Eaton <jwe at bevo.che.wisc.edu> wrote:
>
> On  4-Dec-2007, Søren Hauberg wrote:
>
> | David Bateman skrev:
> | > Kim Hansen wrote:
> | >> I think it is dangerous to have different rules for sparse and full
> | >> matrices combined with the auto conversion from sparse to full.
> | >>
> | > Is it better to be consistent with full matrices or with matlab? At this
> | > point I think it might be better to be consistent with matlab.
> | >
> | I think this sounds kinda like a bug in matlab. It's a bit odd that
> | sparse matrices behave differently then full ones. So, I'd prefer
> | consistency with full matrices.
>
> I agree that it seems like odd behavior for
>
>   x = ones ([2, 0]); x(1) = 1
>
> to fail, but the idea seems to be that when an operation like
>
>   x(idx) = val
>
> forces X to be resized and X starts out empty, Matlab always wants to
> create a row vector, and that is only allowed to happen if all the
> dimensions are zero, or if there are only two dimensions and the first
> dimension is zero or one.  You can avoid the error if you specify both
> rows and columns in your index.  For example,
>
>   x = ones ([2, 0]); x(1,) = 1
>
> succeeds.
>
> We have discussed this topic at least once before.  Please see this
> message:
>
>   http://www.nabble.com/unable-to-resize-A-tf4492119.html#a12811565
>
> OTOH, it succeeds with sparse matrices, so maybe the behavior for full
> matrices is not intentional.  Or, maybe the behavior for sparse
> matrices is not intentional.  Who knows?  If you want to avoid
> problems, you could use two indices unless you know the indexed object
> can't be empty.

I don't really care about what the matrices do when expanded with a
single index, but I think full and sparse should do the same in all
instances where they are expanded with two indices.

The only case where my test fails now is:

octave:20> x=sparse(ones(2)), x([],:)=2
x =

Compressed Column Sparse (rows = 2, cols = 2, nnz = 4)

  (1, 1) ->  1
  (2, 1) ->  1
  (1, 2) ->  1
  (2, 2) ->  1

x =

Compressed Column Sparse (rows = 2, cols = 2, nnz = 0)


octave:21>

-- 
Kim Hansen
Vadgårdsvej 3, 2.tv
2860 Søborg
Fastnet: 3956 2437  --  Mobil: 3091 2437



More information about the Bug-octave mailing list