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 Comments

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.

Sign in to comment.

Answers (0)

Categories

Find more on Sparse Matrices in Help Center and File Exchange

Asked:

on 26 Sep 2019

Commented:

on 26 Sep 2019

Community Treasure Hunt

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

Start Hunting!