Thread Subject: sparse matrix assignment problem

Subject: sparse matrix assignment problem

From: Deepak

Date: 9 Dec, 2007 15:10:14

Message: 1 of 6

I have to create a sparse matrix Y using only some of the
elements from sparse matrices K and M.

The elements I want to include in Y are stored in the array
"freedofs".

I am doing the following to assemble Y:

Y(freedofs,freedofs)=K(freedofs,freedofs)-(omega)^2*M(freedofs,freedofs)

This instantly gives me an "Out of Memory" error.

I am able to see what the entries of the matrix should be by
evaluating

K(freedofs,freedofs)-(omega)^2*M(freedofs,freedofs)

So, apparently, it's the assignment to Y which causes the
out of memory error.

Also, I am easily able to make the Y matrix if I include all
elements of K and M by doing

Y = K-(omega)^2*M


What might be the problem in assigning values to certain
locations of Y by using Y(freedofs,freedofs).

 

Subject: sparse matrix assignment problem

From: Bruno Luong

Date: 9 Dec, 2007 16:17:34

Message: 2 of 6

"Deepak " <deepak.trivedi@gmail.com> wrote in message
<fjh0gm$f5f$1@fred.mathworks.com>...

>
> What might be the problem in assigning values to certain
> locations of Y by using Y(freedofs,freedofs).
>
>

This could be due to an overflow of linear indices. You
should absolutely avoid to program in such way that MATLAB
must use linear indice that is larger than intmax()
(=2147483647).

n=50000;
i=1:n;
j=1:n;
M=sparse(i,j,i);

>> M(double(intmax))

ans =

     0

>> M(double(intmax)+1)
??? Maximum variable size allowed by the program is exceeded.

BTW, are you working on EM integral operators?

Bruno

Subject: sparse matrix assignment problem

From: Jos

Date: 9 Dec, 2007 21:15:42

Message: 3 of 6

"Deepak " <deepak.trivedi@gmail.com> wrote in message
<fjh0gm$f5f$1@fred.mathworks.com>...
> I have to create a sparse matrix Y using only some of the
> elements from sparse matrices K and M.
>
> The elements I want to include in Y are stored in the
array
> "freedofs".
>
> I am doing the following to assemble Y:
>
> Y(freedofs,freedofs)=K(freedofs,freedofs)-(omega)^2*M
(freedofs,freedofs)
>
> This instantly gives me an "Out of Memory" error.
>
> I am able to see what the entries of the matrix should be
by
> evaluating
>
> K(freedofs,freedofs)-(omega)^2*M(freedofs,freedofs)
>
> So, apparently, it's the assignment to Y which causes the
> out of memory error.
>
> Also, I am easily able to make the Y matrix if I include
all
> elements of K and M by doing
>
> Y = K-(omega)^2*M
>
>
> What might be the problem in assigning values to certain
> locations of Y by using Y(freedofs,freedofs).
>
>

I think the problem is in your use of indices. For
instance, I suspect that you think the following will give
you free numbers:

  A = rand(5) ;
  ind = 1:3 ;
  A(ind,ind)

which does not! Try to use linear indexing, using SUB2IND:

tempind = sub2ind(size(A),ind,ind) ;
A(tempind)

hth
Jos

Subject: sparse matrix assignment problem

From: Deepak

Date: 9 Dec, 2007 21:50:17

Message: 4 of 6

Thanks Jos,

I don't want free indices. In fact, I want to do exactly the
same thing that you showed in your first example, i.e.,

> A = rand(5) ;
> ind = 1:3 ;
> A(ind,ind)

Should indeed produce a 3x3 matrix A. Similarly, if I do

ind = [1 2 5]
A(ind,ind)

I intend to get a 3x3 matrix containing elements that are on
both 1st, 2nd and 5th rows and columns.

One solution to the problem is of course to use loops, but
that would considerably slow everything down.

Any other ideas?

Regards
Deepak



"Jos " <DELjos@jasenDEL.nl> wrote in message
<fjhltu$7jv$1@fred.mathworks.com>...
> "Deepak " <deepak.trivedi@gmail.com> wrote in message
> <fjh0gm$f5f$1@fred.mathworks.com>...
> > I have to create a sparse matrix Y using only some of the
> > elements from sparse matrices K and M.
> >
> > The elements I want to include in Y are stored in the
> array
> > "freedofs".
> >
> > I am doing the following to assemble Y:
> >
> > Y(freedofs,freedofs)=K(freedofs,freedofs)-(omega)^2*M
> (freedofs,freedofs)
> >
> > This instantly gives me an "Out of Memory" error.
> >
> > I am able to see what the entries of the matrix should be
> by
> > evaluating
> >
> > K(freedofs,freedofs)-(omega)^2*M(freedofs,freedofs)
> >
> > So, apparently, it's the assignment to Y which causes the
> > out of memory error.
> >
> > Also, I am easily able to make the Y matrix if I include
> all
> > elements of K and M by doing
> >
> > Y = K-(omega)^2*M
> >
> >
> > What might be the problem in assigning values to certain
> > locations of Y by using Y(freedofs,freedofs).
> >
> >
>
> I think the problem is in your use of indices. For
> instance, I suspect that you think the following will give
> you free numbers:
>
> A = rand(5) ;
> ind = 1:3 ;
> A(ind,ind)
>
> which does not! Try to use linear indexing, using SUB2IND:
>
> tempind = sub2ind(size(A),ind,ind) ;
> A(tempind)
>
> hth
> Jos

Subject: sparse matrix assignment problem

From: Bruno Luong

Date: 9 Dec, 2007 21:56:51

Message: 5 of 6

"Jos " <DELjos@jasenDEL.nl> wrote in message
<fjhltu$7jv$1@fred.mathworks.com>...


> I think the problem is in your use of indices. For
> instance, I suspect that you think the following will give
> you free numbers:
>
> A = rand(5) ;
> ind = 1:3 ;
> A(ind,ind)
>
> which does not! Try to use linear indexing, using SUB2IND:
>
> tempind = sub2ind(size(A),ind,ind) ;
> A(tempind)
>

Yes, it seems the indexes are not used as OP intended
(thought the intention was not spelled out explicitly). But
I would still suggest to avoid using sub2ind() all together
for sparse matrix for the very reason I stated above. MATLAB
provides enough built-in functions to manipulate comfortably
sparse matrix (nnz, find with two outputs, sparse function,
union with optional 'rows' argument, etc...) without sub2ind().

Bruno

Subject: sparse matrix assignment problem

From: Bruno Luong

Date: 9 Dec, 2007 22:20:24

Message: 6 of 6

"Deepak " <deepak.trivedi@gmail.com> wrote in message
<fjhnup$mem$1@fred.mathworks.com>...
>
> I don't want free indices. In fact, I want to do exactly the
> same thing that you showed in your first example, i.e.,
>
> > A = rand(5) ;
> > ind = 1:3 ;
> > A(ind,ind)
>

OK! Now it's clear.

May be this would do:

[m n]=size(K);

Ysubs=K(freedofs,freedofs)-(omega)^2*M(freedofs,freedofs);
[i j]=find(Ysubs);
Y=sparse(freedofs(i),freedofs(j),nonzeros(Ysubs), m, n);

Bruno

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
linear indexing Jos 9 Dec, 2007 16:20:13
memory Deepak 9 Dec, 2007 10:15:06
sparse Deepak 9 Dec, 2007 10:15:05
rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com