将稀疏矩阵形式的matlab格式转换为ijv坐标格式

问题描述 投票:1回答:1

我有matlab格式的稀疏矩阵,我想将它们转换为按行排序的ijv坐标格式(第一列)。下面是我的尝试,但它不是那么有效,因为矩阵非常大(有时nnz> 1M),这段代码需要大量内存。任何更好的主意都非常感谢。

function printIJV(file_name,S)
  % S is the sparse matrix in Matlab storing only nonzero values and their indices.e.g (1,1) 20 , (2,1) 30 , (3,2) 22 ,...
  [i,j,v] = find(S);
 temp2 = [i,j,v];
 temp = sortrows(temp2 ,temp2(1) );
  file_id = fopen(file_name,'wt');
  % header: n-rows n-columns n-values
  fprintf(file_id,'%d %d %d\n', size(S,1) ,size(S,2),size(v,1) );
  n = size(temp , 1);
  for i=1:n
  %minus one for zero-based indexing
  fprintf(file_id,'%d %d %g\n',temp(i,1)-1 , temp(i,2)-1 , temp(i,3));
  end
  fclose(file_id);
end
matlab sparse-matrix
1个回答
0
投票

你可以试试

[j,i,v] = find(S')
© www.soinside.com 2019 - 2024. All rights reserved.