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
sparse
is to not represent any zero entries. If you want to represent zeroes in the matrix, you might as well go to itsfull
representation. I don't understand why you want to represent zero entries in thesparse
representation. That's not what it was designed for. – rayryengNaN
instead of zero. – rahnema1