How to fastly construct a sparse matrix with each column equal to some sparse vector

Dear community,
I have 2000 sparse arrays H(i), i=1,...2000, with each of size 100000*5, now I'd like to construct a new sparse matrix X of size 100000*2000 with its i-th column equal to the first column of H(i). The simple way is just a loop:
X = sparse(100000,2000);
for i=1:2000
X(:,i) = H(:,1);
end
However, Such an assignment is slow. Is there any better way? Thanks !
Bo

2 comentarios

Fabio Freschi
Fabio Freschi el 26 de Sept. de 2019
Editada: Fabio Freschi el 26 de Sept. de 2019
Not clear: is H an array (you use H(i)) or a matrix (in the loop: H(:,1)). or maybe a cell array where each H{i} is a 100000*5 matrix?
Can you share the data to play with?
Sorry for the unclear statement. H is a sparse matrix of size 100000*5 in the loop and will be assigned by some_function. I want to extract the first column of H to construct a new sparse matrix X, that is, each column of X is the first column of matrix H.
So the simple code is:
X = sparse(100000,2000);
for i = 1:2000
H = some_function(..); % get a new sparse matrix H of size 100000*5
X(:,i) = H(:,1); % extract the first column of H and put it into the i-th column of X
end
It works, but the Code Analyzer in Matlab detects such an index pattern for a sparse matrix that could be slow. I don't know how to accelerate the assignment.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Sparse Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 26 de Sept. de 2019

Comentada:

el 26 de Sept. de 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by