[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: bsxfun and sparse matrices in Matlab
From: |
PhilipNienhuis |
Subject: |
Re: bsxfun and sparse matrices in Matlab |
Date: |
Tue, 4 Oct 2011 04:07:37 -0700 (PDT) |
Jordi Gutiérrez Hermoso-2 wrote:
>
> On 3 October 2011 10:15, PhilipNienhuis <pr.nienhuis@> 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.
>
Hopefully this answers your question a bit better:
(r2009a)
>> x = sprand(1,3,1/3); y = x'
y =
(3,1) 0.1270
>> nnz (bsxfun(@plus, x, y))
ans =
5
>> bsxfun(@plus, x, y)
ans =
(3,1) 0.1270
(3,2) 0.1270
(1,3) 0.1270
(2,3) 0.1270
(3,3) 0.2540
>> nnz (bsxfun(@minus, x, y))
ans =
4
>> bsxfun(@minus, x, y)
ans =
(3,1) -0.1270
(3,2) -0.1270
(1,3) 0.1270
(2,3) 0.1270
>> nnz (bsxfun(@times, x, y))
ans =
1
>> bsxfun(@times, x, y)
ans =
(3,3) 0.0161
>> nnz (bsxfun(@rdivide, x, y))
ans =
7
>> bsxfun(@rdivide, x, y)
ans =
(1,1) NaN
(2,1) NaN
(1,2) NaN
(2,2) NaN
(1,3) Inf
(2,3) Inf
(3,3) 1
>> nnz (bsxfun(@ldivide, x, y))
ans =
7
>> bsxfun(@ldivide, x, y)
ans =
(1,1) NaN
(2,1) NaN
(3,1) Inf
(1,2) NaN
(2,2) NaN
(3,2) Inf
(3,3) 1
>> nnz(bsxfun(@power, x, y))
ans =
7
>> bsxfun(@power, x, y)
ans =
(1,1) 1.0000
(2,1) 1.0000
(1,2) 1.0000
(2,2) 1.0000
(1,3) 1.0000
(2,3) 1.0000
(3,3) 0.7695
>>
P.
--
View this message in context:
http://octave.1599824.n4.nabble.com/bsxfun-and-sparse-matrices-in-Matlab-tp3867746p3870634.html
Sent from the Octave - Maintainers mailing list archive at Nabble.com.