Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
sparse or not to sparse?

Subject: sparse or not to sparse?

From: Baalzamon

Date: 8 Dec, 2011 15:55:09

Message: 1 of 6

Good day folks.

This is my situation:

I have three vectors: x y value.
I need a surface plot, contour etc.
So I made a script that fills out a 2d array with the values.

So problem (1) for some x-y pairs there does not exist a corresponding value, so from preallocating as a zero matrix, I get a default value of zero.

This plays havoc with my surface as the values are normally close together and as such the zeros take away the detail from the rest of the plot...

As a workaround, I replace the zeros with inf, thus surface won't plot them...
Then someone mentioned using sparse....

results:

Z2{i} = myfunc(....)
Z£{i} = sparse( myfunc(...) )

From whos:
  Z2 1x2 1156525768 cell
  Z3 1x2 2313747624 cell


Am I wrong to assume that sparse will save memory?

Subject: sparse or not to sparse?

From: Matt J

Date: 8 Dec, 2011 16:28:08

Message: 2 of 6

"Baalzamon" wrote in message <jbqmkt$raa$1@newscl01ah.mathworks.com>...
>
> As a workaround, I replace the zeros with inf, thus surface won't plot them...
====================

Or, you could use NaN.

> Then someone mentioned using sparse....
>
>
> Am I wrong to assume that sparse will save memory?
==================

Depends on the shape of the Z3{i}. Here's an example showing the sensitivity of sparse matrix memory consumption to matrix shape:

>> A=sparse(zeros(1e6,1)); At=A.';
>> whos
  Name Size Bytes Class Attributes

  A 1000000x1 20 double sparse
  At 1x1000000 4000016 double sparse

Subject: sparse or not to sparse?

From: Steven_Lord

Date: 8 Dec, 2011 16:33:02

Message: 3 of 6



"Baalzamon " <baalzamon_moridin@yahoo.com> wrote in message
news:jbqmkt$raa$1@newscl01ah.mathworks.com...
> Good day folks.
>
> This is my situation:

*snip*

> Am I wrong to assume that sparse will save memory?

It depends. Are your matrices sparsely populated -- in other words, what
percentage of the elements in your matrix are nonzero?

If your matrices are sparsely populated (a small percentage of elements are
nonzero), then the overhead of storing the location information for the
nonzero values will be offset by the gain of not having to store the zero
values. In this case, the sparse matrix will take less space than the full
matrix.
If your matrices are densely populated, then the overhead is NOT offset and
the sparse matrix will take MORE space than the full matrix.

You can see this effect using:

% Create densely populated full matrix and its sparse version
% 100% of elements are nonzero
denselyPopulated = ones(10);
denseOverheadNotOffset = sparse(denselyPopulated);

% Create sparsely populated full matrix and its sparse version
% 1% of elements are nonzero
sparselyPopulated = zeros(10);
sparselyPopulated(5, 7) = 1;
sparseOverheadOffset = sparse(sparselyPopulated);

% Compare
whos

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: sparse or not to sparse?

From: Baalzamon

Date: 8 Dec, 2011 21:40:09

Message: 4 of 6

Ah thanks. They are not that sparse unfortunately...


Apart from swapping my zeros to inf is there another way to avoid zero entries in a surface plot?

Subject: sparse or not to sparse?

From: Baalzamon

Date: 8 Dec, 2011 21:42:08

Message: 5 of 6

"Matt J" wrote in message <jbqoio$4pq$1@newscl01ah.mathworks.com>...
> "Baalzamon" wrote in message <jbqmkt$raa$1@newscl01ah.mathworks.com>...
> >
> > As a workaround, I replace the zeros with inf, thus surface won't plot them...
> ====================
>
> Or, you could use NaN.
>
>

Sorry didn't see this reply....


Many thanks.

Subject: sparse or not to sparse?

From: Steven_Lord

Date: 8 Dec, 2011 21:45:45

Message: 6 of 6



"Baalzamon " <baalzamon_moridin@yahoo.com> wrote in message
news:jbrarp$acs$1@newscl01ah.mathworks.com...
> Ah thanks. They are not that sparse unfortunately...
>
>
> Apart from swapping my zeros to inf is there another way to avoid zero
> entries in a surface plot?

NaN.

[x, y, z] = peaks;
z(abs(z) < 0.1) = NaN;
surf(x, y, z)

There are a couple of connection points where the relevant faces crossed
through, but had no vertices in, the removed zone. Evaluating PEAKS on a
finer grid would probably eliminate most or all of those.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us