0
votes

I have a sparse matrix stored in matlab format with explicit zeros(taking binary values). I want to combine these matrices and convert the result into another sparse matrix format. My attempt is to keep the binary ones vector for zero values and put it zero after calling sparse matrix. Like this:

function matlab_to_HB(matName ,fileName ,title ,  key , type)

data = load(matName);
SMat = data.Problem.A; % the nonzero values
 [i,j,v] = find(SMat);
 if(isfield(data.Problem,'Zeros'))
  zerosMat =  data.Problem.Zeros; % indices of zero values
 [ii,jj,vv] = find(zerosMat); % vv is a vector of ones
  vvSize = size(ii , 1);
ifmt = 8;
job = 2;
rhs = 0;

test2 = sparse([i ; ii],[j;jj],[v;vv]); % sparse immediately removes the explicit zeros. I want to combine the the zeros with the nonzeros.

for idx =1 : vvSize
  test2(ii(idx) , jj(idx)) = 0.0;  
end
     dm2hb(fileName ,test2,rhs,title,key,type,ifmt,job) 

    end

This might not be efficient if the number of explicit zeros is large. ANy better idea

1
The purpose of sparse is to not represent any zero entries. If you want to represent zeroes in the matrix, you might as well go to its full representation. I don't understand why you want to represent zero entries in the sparse representation. That's not what it was designed for.rayryeng
Because of the problem nature... those explicit zeros are required and that is why they stored their indices. Other formats of sparse matrix consider those 'explicit ' zeros.Nawal
You can use NaN instead of zero.rahnema1
What's the difference between an explicit zero and a non explicit one? Both are zeroAnder Biguri
explicit zeros are considered in terms of memory and calculation. They are taken into account like nonzero elements. These terms are used in sparse matrices. If you do a calculation and get a zero from calculation, then this value is considered.Nawal

1 Answers

0
votes

Matlab never allows storing zero entries in its sparse format, that is not convenient in general and can kill performance. Even if you write a mex file and then pass a sparse matrix with some zeros it will choke awfully. Note that deleting nonzero elements is a very inefficient shifting process. If you want to stay in Matlab, the best and efficient way is to store it a full matrix and after adding all elements store it as sparse.

for idx =1 : vvSize
 test2(ii(idx) , jj(idx)) = 0.0;  
end
test2 = sparse([i ; ii],[j;jj],[v;vv]);

I guess you can implement an efficient piece of C code, especially if your matrices are large. If not the above code should work fine.

Another approach would be removing entries directly from the Ax and Ap in Matlab. I don't think that has much of an impact though.