我正在处理相当大的稀疏矩阵,其大小约为150,000 * 150,000。我需要访问其行,提取非零元素并按照以下代码替换这些值:
tic
H = [];
for i = 1: size(A,2)
[a,b,c] = find(A(i,:)); % extract the rows
add = diff([0 c(2:end) 0]); % the replacing rule
aa = i*ones(1,size(a,2)); % return back the old position of rows
G0 = [aa' b' add']; % put it back the old position with replaced values
H = [H; G0];
end
H1 = H(:,1);
H2 = H(:,2);
H3 = H(:,3);
ADD = sparse(H1,H2,H3,nm,nm,nzmax);
toc
我发现find
函数在这段代码中非常耗时(0.1s /行),并且使用稀疏矩阵的当前大小,这项工作需要大约33个小时。我相信有一些方法可以解决,但我是一个新生的编码和处理稀疏矩阵真的很可怕。
你能给我一些想法吗?
答案 0 :(得分:1)
在整个阵列上应用find
函数后,可以使用accumarray
函数,然后使用[a b c]=find(A.');
add=accumarray(b,c,[],@(x){diff([0 ;x(2:end) ;0])});
H = [b a vertcat(add{:})];
在每一行上应用函数:
$headers[] = 'Accept: application/atom+xml';