[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Saving memory in spones()
From: |
Rik |
Subject: |
Re: Saving memory in spones() |
Date: |
Mon, 16 May 2011 09:27:14 -0700 |
On 05/13/2011 10:02 AM, John W. Eaton wrote:
> On 12-May-2011, Rik wrote:
>
> | 5/12/11
> |
> | The spones() function returns a sparse matrix with a 1 in each non-zero
> | entry in the matrix. It is useful for viewing the structure of a matrix,
> | but obviously all of the actual values are lost.
> |
> | The code is simple enough to quote in its entirety:
> |
> | [i, j, v] = find (S);
> | [m, n] = size (S);
> |
> | r = sparse (i, j, 1, m, n);
> |
> | Question: Why not return a sparse logical matrix rather than a sparse
> | double matrix and save 7 bytes per entry?
> |
> | r = sparse (i, j, logical (1), m, n);
>
> The spones function also exists in Matlab and it apparently returns a
> sparse matrix with class double. I suppose it could cause some
> compatibility problems if Octave behaved differently.
>
> jwe
5/16/11
John,
It's e-mail which does a bad job of conveying nuance so I couldn't quite
tell whether you think this would be a bad idea or not.
>From my perspective, I think Mathworks made the expedient design decision
at the time and just returned a sparse double. They made one choice, but I
don't think we need to slavishly follow them if we have good reasons not
to. In this case it saves 7/8 of the memory (88% savings) to use a logical
type. In addition, I think there's very little room for compatibility
problems because the matrix type will automatically be promoted to double
if a user tries an operation, such as sum, to see how many entries are in
each column.
--Rik