How to speed up making large matrices, and reduce memory usage
4 views (last 30 days)
Show older comments
In-chan Kim
on 6 May 2020
Commented: In-chan Kim
on 6 May 2020
How can I speed up making a large matrix, and also cut down memory usage?
I need to make a matrix which turns out to be in the order of 2,000 by 30,000 ish.
I can run it, but it'd be great to be able to speed it up, and also reduce memory use.
At the moment my code is as below, there length of C is 1800.
Creating Aineq is my final aim!
Aineq=[ones(length(C))]; %size of matrix to represent size one of the decision variables Q or G
Aineq1=[tril(Aineq),-tril(Aineq),tril(Aineq),-tril(Aineq),Aineq*0,Aineq*0,Aineq*0,zeros((length(C)),length(F))]; %1 matrix for first DV, then the next DV
Aineq2=[-tril(Aineq),tril(Aineq),-tril(Aineq),tril(Aineq),Aineq*0,Aineq*0,Aineq*0,zeros((length(C)),length(F))]; %2
Aineq3=[eye(length(C)),eye(length(C)),Aineq*0,Aineq*0,Aineq*0,Aineq*0,Aineq*0,zeros((length(C)),length(F))]; %3
Aineq4=[tril(Aineq,-1)*-1,tril(Aineq),tril(Aineq,-1)*-1,tril(Aineq,-1),Aineq*0,Aineq*0,Aineq*0,zeros((length(C)),length(F))]; %4
Aineq5=[Aineq*0,Aineq*0,Aineq*0,eye(length(C)),Aineq*0,Aineq*0,Aineq*0,zeros((length(C)),length(F))]; %5
Aineq6=[Aineq*0,Aineq*0,tril(Aineq,-1)*-1,tril(Aineq,-1),tril(Aineq,-1),tril(Aineq,-1)*-1,tril(Aineq,-1)*-1,zeros((length(C)),length(F))]; %6
Aineq7=[Aineq*0,Aineq*0,tril(Aineq)*-1,tril(Aineq),tril(Aineq),tril(Aineq)*-1,tril(Aineq)*-1,zeros((length(C)),length(F))]; %7
Aineq8=[Aineq*0,Aineq*0,tril(Aineq,-1),tril(Aineq,-1)*-1,tril(Aineq,-1)*-1,tril(Aineq,-1),tril(Aineq,-1),zeros((length(C)),length(F))]; %8
Aineq9=[Aineq*0,Aineq*0,tril(Aineq),tril(Aineq)*-1,tril(Aineq)*-1,tril(Aineq),tril(Aineq),zeros((length(C)),length(F))];%9
Aineq10=[Aineq*0,Aineq*0,eye(length(C)),Aineq*0,Aineq*0,Aineq*0,Aineq*0,zeros((length(C)),length(F))]; %10
Aineq11=[Aineq*0,Aineq*0,Aineq*0,eye(length(C)),Aineq*0,Aineq*0,Aineq*0,zeros((length(C)),length(F))]; %11
Aineq12=[Aineq*0,Aineq*0,Aineq*0,Aineq*0,Aineq*0,eye(length(C)),Aineq*0,zeros((length(C)),length(F))]; %12
Aineq13=[Aineq*0,Aineq*0,Aineq*0,Aineq*0,eye(length(C)),Aineq*0,Aineq*0,zeros((length(C)),length(F))]; %13
Aineq14=[Aineq*0,Aineq*0,Aineq*0,Aineq*0,Aineq*0,Aineq*0,eye(length(C)),zeros((length(C)),length(F))]; %14
Aineq15=[Aineq*0,Aineq*0,Aineq*0,Aineq*0,Aineq*0,Aineq*0,eye(length(C))*-1,zeros((length(C)),length(F))]; %15
clear Aineq
% Aineq=[Aineq1;Aineq2;Aineq3;Aineq4;Aineq5;Aineq6;Aineq7;Aineq8;Aineq9;Aineq10;Aineq11;Aineq12;Aineq13;Aineq14;Aineq15];
Aineq=[Aineq1]; clear Aineq1
Aineq=[Aineq;Aineq2]; clear Aineq2
Aineq=[Aineq;Aineq3]; clear Aineq3
Aineq=[Aineq;Aineq4]; clear Aineq4
Aineq=[Aineq;Aineq5]; clear Aineq5
Aineq=[Aineq;Aineq6]; clear Aineq6
Aineq=[Aineq;Aineq7]; clear Aineq7
Aineq=[Aineq;Aineq8]; clear Aineq8
Aineq=[Aineq;Aineq9]; clear Aineq9
Aineq=[Aineq;Aineq10]; clear Aineq10
Aineq=[Aineq;Aineq11]; clear Aineq11
Aineq=[Aineq;Aineq12]; clear Aineq12
Aineq=[Aineq;Aineq13]; clear Aineq13
Aineq=[Aineq;Aineq14]; clear Aineq14
Aineq=[Aineq;Aineq15]; clear Aineq15
0 Comments
Accepted Answer
Bjorn Gustavsson
on 6 May 2020
This looks like a very sparse matrix, right? Then I would first figure out how to assign the blocks of Aineq from its components, something like this:
blksz = [3,13]; % or whatever they become
nBlks = 15;
Aineq = zeros(blksz*nBlks);
for i1 = 1:nBlks
for i2 = 1:nBlks
% Some logics here for selecting which block to fill Aineq with
Aineq((1:blksz(1))+blksz*(i1-1),(1:blksz(2))+blksz(2)*(i2-1)) = CurrBlk;
end
end
Once you've figured that one out, you should look at what components of every block is non-zero and start to build sparse blocks, and then concatenate them instead, that would be much more memmory efficient. Then the last step (or preferably the firts, but I find it easier to move to sparse representations of comlex matrices step-by-step), is to calculate vectors with the row and column-indices and the corresponding matrix element and then simply concatenate those into three arrays i_rows, i_cols, val and then call sparse:
Asparse = sparse(i_rows,i_cols,vals,nRows,nCols);
HTH
8 Comments
More Answers (0)
See Also
Categories
Find more on Sparse Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!