How to fill up a huge/very large matrix, with elements from another matrix in a time efficient way?
Show older comments
Hi there,
I am doing some calculations for some graph/network with vertices that have certain connections/edges to each other.
My goal is to set-up a very large matrices with dimensions ranging from roughly 90000x300(works but already takes up ~4.6 seconds to create) up to 9,000,000 (9mln)x30000 elements in an efficient way, such that is does not take forever to fill up the matrix. Most elements are, however, zeros so I tried using sparse matrices, but sparse matrices are not suitable if you want to fill them up element-wise.
The size of the matrices depend on a discretization parameter(delta) that I choose, I need to make it as small as possible, down to 10^-4. My function is now as follows(I use Hungarian notation):
function mZ_j = createZ_j(iN, iP, iD_j, mX, vPA)
% Function that creates matrix Z_j.
% Dimensions: (n-p)x(p*d_j)+1, d_j can go up to 6, but is 2 on average
% input/starting parametes are:
% delta = 0.0001, s=0.5, T=900(15 min in seconds), d_j=2
iRows = iN-iP; % n:= T/delta, p:=s/delta
iCol = (iP*iD_j);
mZ_j = zeros(iRows,iCol); % (n-p)x(p*d_j) matrix
mX_j = mX(:,vPA); % matrix were elements will be taken from, vPA(size iD_j) has the column indexes
p = iP+1; % indicator for each row; it is an autoregressive process and each row you regress on p lags, but every row you take one next step in time
for i=1:iRows
for j=1:iP
iCStart = ((j-1)*iD_j)+1; iCEnd = j*iD_j;
mZ_j(i,iCStart:iCEnd) = mX_j(p-j,:);
end
p = p+1;
end
Does anyone see or know how I can assign the elements from mX_j more efficiently to mZ_j? The operations themselve do not take a lot time, it is just the amount of iterations that it has to do.
Thanks!
6 Comments
Guillaume
on 27 Jun 2019
Questions:
- Is mX sparse? Otherwise, I don't see where the sparsity comes from in mZ_j.
- isn''t iD_j equal to numel(vPA)
- You're filling up your result and reading your input by rows. It would be faster to do it by columns. could the mX input be generated transposed and the mz_J output used transposed? Even better would be to have mx = fliplr(mx.')
I forgot to ask:
- is iN equal to size(mX, 1)+1 or in any way related to the height of mX
It's fairly trivial to construct mz_J without a loop as a full matrix with simple indexing. The most expensive operation, if you're required to operate along the rows, is going to be some transpose to temporary operate along columns. However, as point out by Steven, a 9 million by 30000 matrix is going to require massive amounts of memory.
As for the math question, I'm the wrong person to ask I'm afraid.
Jeroen
on 27 Jun 2019
Walter Roberson
on 27 Jun 2019
Inverse of a sparse matrix is typically dense. Inverse of a nonsquare matrix is not defined. But perhaps you are doing something like A*A' that will be square but possibly large.
Inverse of a large matrix that is not just 0 and 1 will typically have terrible condition number and give numeric garbage. Don't use inv unless you are forced to by external requirements. Use other techniques such as \ instead.
Accepted Answer
More Answers (0)
Categories
Find more on Logical 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!
