在MATLAB中进行稀疏矩阵的单元乘法和堆叠的有效方法

时间:2018-11-12 12:43:57

标签: matlab performance cell sparse-matrix

我所拥有的是一个大小为J的单元格1xs,其中填充了大小为mxnm>=n)的稀疏矩阵,以及两个完整的矩阵r和大小分别为lmxcxp的{​​{1}}。尺寸大概是

sxcxp

到目前为止,我的工作是这样做(请参见下面的代码),但这似乎效率很低。我正在寻找一种方法,以一种可扩展的方式加快工作速度,因为我需要多次执行此操作(s = 4; % or 9 m = 10000; % can increase up to 300k n = 36; % can increase up to m c = 3; p = 25; 最多也可以增加到300k),因此要尽快执行此步骤,很好,因为到目前为止这似乎是瓶颈。这里是我需要做的代码:

m

此外,我发现这种方式没有嵌套的J = cell(s, 1); % just fill J with sparse matrices of size mxn. Each sparse matrix is different for each cell, but all have the same nnz. J(:) = {sprand(m,n,0.1)}; r = rand(m, c, p); l = rand(s, c, p); % preallocate vectors row_vec = zeros(nnz(J{1}),c*p); col_vec = zeros(nnz(J{1}),c*p); val_vec = zeros(nnz(J{1}),c*p); % do computation for pi = 1:p for ci = 1:c J_ = 0; for si=1:s % multiply each sparse matrix in cell with scalar l(si,ci,pi) and sum them up J_ = J_ + J{si} * l(si,ci,pi); end % multiply resulting sparse matrix with diagonal matrix (resulting from vector r(:,ci,pi)) and get final indices for later [row_vec_temp, ... col_vec(:,(pi-1)*c+ci), ... val_vec(:,(pi-1)*c+ci)] = find(spdiags(r(:,ci,pi),0,m,m) * J_); row_vec(:,(pi-1)*c+ci) = row_vec_temp + row_vec(end,max(1,(pi-1)*c+ci-1)); end end % build final stacked sparse matrix of size m*c*pxn using calculated indices. J_final = sparse(row_vec, col_vec, val_vec); 循环,但这似乎效率更低:

for

第一个版本大约需要0.27s,第二个版本大约需要0.3s。

0 个答案:

没有答案