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 matrices in for loops

Subject: sparse matrices in for loops

From: Marios Karaoulis

Date: 3 Nov, 2010 23:30:21

Message: 1 of 3

I have a a sparse matrix that I create this way
 
node_ebc is a int vector

k=sparse(a_index,b_index,val_index,num_nodes,num_nodes);

then I execute this loop

tmp2=setdiff([1:1:num_nodes],node_ebc);
k(node_ebc,tmp2)=0;
k(tmp2,node_ebc)=0;

k(sub2ind(size(k),node_ebc,node_ebc))=1;


Obviously, since I zero elements inside k matrix, which are allready zero, matlab allocates space for this matrix, which is really slow.

How can I speed up things using the a_index and b_index?

Subject: sparse matrices in for loops

From: Sean

Date: 4 Nov, 2010 14:43:03

Message: 2 of 3

"Marios Karaoulis" <marios_hellas_23@yahoo.gr> wrote in message <iasrad$4gd$1@fred.mathworks.com>...
> I have a a sparse matrix that I create this way
>
> node_ebc is a int vector
>
> k=sparse(a_index,b_index,val_index,num_nodes,num_nodes);
>
> then I execute this loop
>
> tmp2=setdiff([1:1:num_nodes],node_ebc);
> k(node_ebc,tmp2)=0;
> k(tmp2,node_ebc)=0;
>
> k(sub2ind(size(k),node_ebc,node_ebc))=1;
>
>
> Obviously, since I zero elements inside k matrix, which are allready zero, matlab allocates space for this matrix, which is really slow.
>
> How can I speed up things using the a_index and b_index?

Skip the whole indexing expression just like M-LINT (at least on my version) is hinting at:

one2n = [1:1:num_nodes].'; %list of indices
tmp2=setdiff(one2n,node_ebc); %different ones
aidx = [a_index;one2n;node_ebc;node_ebc]; %concatenated list of row indexes
bidx = [b_index;node_ebc;one2n;node_ebc];
vidx = [val_index;zeros(2*num_nodes,1);ones(num_nodes,1)];
k=sparse(aidx,bidx,vidx,num_nodes,num_nodes); %make sparse



%not tested in ML

Subject: sparse matrices in for loops

From: Sean

Date: 4 Nov, 2010 15:45:06

Message: 3 of 3


> Skip the whole indexing expression just like M-LINT (at least on my version) is hinting at:
>
> one2n = [1:1:num_nodes].'; %list of indices
> tmp2=setdiff(one2n,node_ebc); %different ones
> aidx = [a_index;one2n;node_ebc;node_ebc]; %concatenated list of row indexes
> bidx = [b_index;node_ebc;one2n;node_ebc];
> vidx = [val_index;zeros(2*num_nodes,1);ones(num_nodes,1)];
> k=sparse(aidx,bidx,vidx,num_nodes,num_nodes); %make sparse
>
>
>
> %not tested in ML

I just realized you're going to have to put a call to unique in before calling sparse. I had thought that only the second value would count i.e.
sparse([1;1],[2;2],[2;7],3,3)
would be 7, but it adds them and is thus 9.

To fix:
[u, ix] = unique([aidx, bidx],'rows','first'); %call first one because it's the one you want to get rid of.

aidx(ix) = []; %remove the first one
bidx(ix) = [];
vidx(ix) = [];

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