Thread Subject: extracting elements from a large sparse matrix

Subject: extracting elements from a large sparse matrix

From: psychoblade55@hotmail.com

Date: 8 Jun, 2007 18:54:59

Message: 1 of 6

Hi all,
I have a large (order 10^5 by 10^5) sparse matrix A and a pair of
vectors r,c, of length maybe order 100 or so. Is there an easy way
get a vector of the form

A(r(1), c(1))
A(r(2), c(2))
...
A(r(end),c(end))

without using sub2ind or ind2sub? The matrix is too large for me to
do something like

i = sub2ind(size(A),r,c);
v = A(i);

any other suggestions?
Thank you all, SF

Subject: extracting elements from a large sparse matrix

From: Jos

Date: 10 Jun, 2007 08:08:46

Message: 2 of 6

psychoblade55 wrote:
>
>
> Hi all,
> I have a large (order 10^5 by 10^5) sparse matrix A and a pair of
> vectors r,c, of length maybe order 100 or so. Is there an easy way
> get a vector of the form
>
> A(r(1), c(1))
> A(r(2), c(2))
> ...
> A(r(end),c(end))
>
> without using sub2ind or ind2sub? The matrix is too large for me
> to
> do something like
>
> i = sub2ind(size(A),r,c);
> v = A(i);
>
> any other suggestions?
> Thank you all, SF
>
>

Too large? Really? Anw why did you consider using ind2sub?
Just sub2ind a try ...

Jos

Subject: extracting elements from a large sparse matrix

From: Jos

Date: 10 Jun, 2007 08:09:15

Message: 3 of 6

psychoblade55 wrote:
>
>
> Hi all,
> I have a large (order 10^5 by 10^5) sparse matrix A and a pair of
> vectors r,c, of length maybe order 100 or so. Is there an easy way
> get a vector of the form
>
> A(r(1), c(1))
> A(r(2), c(2))
> ...
> A(r(end),c(end))
>
> without using sub2ind or ind2sub? The matrix is too large for me
> to
> do something like
>
> i = sub2ind(size(A),r,c);
> v = A(i);
>
> any other suggestions?
> Thank you all, SF
>
>

Too large? Really? Anw why did you consider using ind2sub?
Just give sub2ind a try ...

Jos

Subject: extracting elements from a large sparse matrix

From: John D'Errico

Date: 10 Jun, 2007 20:02:44

Message: 4 of 6

psychoblade55 wrote:
>
>
> Hi all,
> I have a large (order 10^5 by 10^5) sparse matrix A and a pair of
> vectors r,c, of length maybe order 100 or so. Is there an easy way
> get a vector of the form
>
> A(r(1), c(1))
> A(r(2), c(2))
> ...
> A(r(end),c(end))
>
> without using sub2ind or ind2sub? The matrix is too large for me
> to
> do something like
>
> i = sub2ind(size(A),r,c);
> v = A(i);
>
> any other suggestions?
> Thank you all, SF

The reason why sub2ind fails here is because
a linear indexing fails. With a 10^5x10^5
matrix, there are 10^10 terms. But indexes
in Matlab will only go up to 2^32-1, too
small to index that large of an array with
only one index.

With only 100 or so elements to extract,
I might extract the specific columns first.
Then use sub2ind on the resulting smaller
array. You will need to modify the column
index. But you could write a little function
to do so.

function Aij = extractelements(A,i,j)
% extracts elements from large sparse arrays.
% A - any array
% i,j - vectors of row and column indices
Acols = A(:,j);
nj = length(j);
index = sub2ind([size(A,1),nj],i,1:nj);
Aij = Acols(index);

This function should extract elements
from arrays too large to index otherwise.

HTH,
John

Subject: extracting elements from a large sparse matrix

From: Jos

Date: 11 Jun, 2007 04:59:17

Message: 5 of 6

John D'Errico wrote:
>
> The reason why sub2ind fails here is because
> a linear indexing fails. With a 10^5x10^5
> matrix, there are 10^10 terms. But indexes
> in Matlab will only go up to 2^32-1, too
> small to index that large of an array with
> only one index.

You're right John. My reply to the OP was too hasty.

Jos

Subject: extracting elements from a large sparse matrix

From: Marcus M. Edvall

Date: 11 Jun, 2007 15:51:24

Message: 6 of 6

The 64-bit version of MATLAB might do the trick (from R2006b), but
check with MW.

Best wishes, Marcus
Tomlab Optimization Inc.
 <http://tomopt.com/>

Tags for this Thread

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.

rssFeed for this Thread

Contact us at files@mathworks.com