找到你要的答案

Q:equivalent code to Sparse function matlab

Q:等效代码稀疏的MATLAB函数

I want to translate a Matlab code to C++. I use for this the Matlab-Coder. However this Matlab code uses the function Sparse(triplet) that is not performed by the Matlab coder. So i decided to write my own Sparse function in order to be translated by the Matlab coder :

function [ind,values]=test_new3_sparse(i,j,v,m,n)
  if(isrow(i)==0) i=i'; end
  if(isrow(j)==0) j=j'; end
  if(isrow(v)==0) v=v'; end
 
  k=1;
  A=zeros(m,n); %this line makes a problem when "m lines and the n 
                %columns" are too big

  for l=1:size(i,2)
   A(i(l),j(l))=v(l);
  end

  ind=zeros(m,2);
  values=zeros(1,m);

  for i=1 : size(A,1)
     for j=1:size(A,2)
         if A(i,j) ~=0
             ind(k,1)=i;
             ind(k,2)=j;
             values(k)=A(i,j); k=k+1;            
         end
     end
   end
  
end

This function I wrote works well when n and m are not important. However i have the error "out of memory" when they are too big. Any suggestions?

我想翻译的matlab代码,C++。我用这个matlab编。然而这个matlab代码使用函数稀疏(三)不是由MATLAB编码器进行。所以我决定写我自己的稀疏的功能是为了通过MATLAB编译:

function [ind,values]=test_new3_sparse(i,j,v,m,n)
  if(isrow(i)==0) i=i'; end
  if(isrow(j)==0) j=j'; end
  if(isrow(v)==0) v=v'; end
 
  k=1;
  A=zeros(m,n); %this line makes a problem when "m lines and the n 
                %columns" are too big

  for l=1:size(i,2)
   A(i(l),j(l))=v(l);
  end

  ind=zeros(m,2);
  values=zeros(1,m);

  for i=1 : size(A,1)
     for j=1:size(A,2)
         if A(i,j) ~=0
             ind(k,1)=i;
             ind(k,2)=j;
             values(k)=A(i,j); k=k+1;            
         end
     end
   end
  
end

我写的这个函数在N和m不重要的时候效果很好。然而,我有错误的“内存不足”时,他们太大。有什么建议吗?

c++  matlab  sparse-matrix