[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: bsxfun and sparse matrices in Matlab
From: |
Jordi Gutiérrez Hermoso |
Subject: |
Re: bsxfun and sparse matrices in Matlab |
Date: |
Mon, 3 Oct 2011 12:20:03 -0500 |
On 3 October 2011 10:15, PhilipNienhuis <address@hidden> wrote:
>
> Jordi Gutiérrez Hermoso-2 wrote:
>>
>> While testing more things about auto BSX, I noticed that I missed to
>> overload the operators for sparse matrices, and while investigating
>> *that*, it seems like bsxfun destroys sparsity. While this may make
>> sense for some operators (e.g. power operators), I wanted to confirm
>> if this is what Matlab does.
>>
>> Can someone please confirm if the following does not produce sparse
>> matrices?
>>
>> x = sprand(1,3,1/3); y = x';
>> bsxfun(@plus, x, y)
>> bsxfun(@minus, x, y)
>> bsxfun(@times, x, y)
>> bsxfun(@rdivide, x, y)
>> bsxfun(@ldivide, x, y)
>> bsxfun(@power, x, y)
>>
>>
>
> At work (ML r2009a):
>
>>> x = sprand(1,3,1/3); y = x'
> y =
> (3,1) 0.1270
>>> issparse (bsxfun(@plus, x, y)
> ans =
> 1
>>> issparse (bsxfun(@minus, x, y))
> ans =
> 1
>>> issparse (bsxfun(@times, x, y))
> ans =
> 1
>>> issparse (bsxfun(@rdivide, x, y))
> ans =
> 1
>>> issparse (bsxfun(@ldivide, x, y))
> ans =
> 1
>>> issparse (bsxfun(@power, x, y))
> ans =
> 1
>
> (empty lines removed)
I see. Thanks. Can you please also check what the nnz for the results
are? I see here in the Octave source a comment about how Matlab
ignores zero elements in sparse matrices when doing elementwise
calculations. Which would produce very weird results for some of the
bsxfun outputs above.
This is probably a very minor concern. I can't imagine situations
where you would want to use a lot of BSX with sparse matrices... most
BSX operations should destroy sparsity.
- Jordi G. H.