Search code examples
matlabsparse-matrix

Normalizing a huge sparse matrix


I have an array of high dimensional however very sparse matrices. I want to normalize them so that column sums of all matrices sum to one.

Here is the sample code I use:

bg = matrices{1};
for i = 2:length(matrices) , bg = bg + matrices{i}; end
normalizer = sum(bg);
for i = 1:length(matrices) 
    for j = 1:size(matrices{i},1)
        matrices{i}(j,:) = matrices{i}(j,:) ./ normalizer;
    end
end

However as you can guess this is very slow. One alternative is:

for i = 1:length(matrices) 
    matrices{i} = matrices{i} ./ repmat(normalizer,size(matrices{i},1),1);
end

but this halts because there is not enough memory to create a huge and nearly full matrix (repeated with normalizer)

Can you suggest a better alternative?


Solution

  • If you converted your problem to a single sparse matrix, then you could use

    bsxfun(@rdivide,A,normalizer);