Search code examples
matlabmatrixsparse-matrixadjacency-matrixbsxfun

How to avoid a for-loop in MATLAB, when performing a operation on each row in a very large matrix?


I am using MATLAB. I have very large sparse matrices, and I want to perform a logical or bsxfun on each column of this matrix. There is a single for loop where in it is a single operation of logical to fill a sparse matrix. Here I include a sample function with some fake small data to see what I am trying to do;

function maskMat()
graph_temp = round(rand(10,10));
tic;
com_mat = round(rand(10,10));
com = round(rand(10,1));
for ii=1:length(graph_temp)    
    com_mat(:,ii) = logical(com ~= com(ii));
    %bsxfun works too but is slightly slower
    %com_mat(:,ii) =  bsxfun(@ne,com,com(ii));
end
toc;
com_mat = graph_temp .* com_mat;

graph_temp and com_mat are much bigger with around 1M rows and columns and the code is horribly slow do to the for loop. There is another relevant question here on SO, but I have not understood the theory behind it to see if I can apply those solutions to this problem as well.

I was thinking to either write a mex c++ function or try to get some sort of nested arrayfun so that each logical/bsxfun operation is called as a subroutine of a greater function to avoid the for loop bottle neck.


Solution

  • I'm not sure I followed your code all the way. So, to make sure, is com_mat(ii,jj) equals to com(ii) ~= com(jj)?

    If so try the following options

    com_mat = bsxfun( @ne, com, com' ); %' creates entire matrix at once
    com_mat = graph_temp .* com_mat;  % do the masking
    

    Since you are dealing with sparse matrices, why don't you take advantage of it

    [ii jj] = find( graph_temp );
    [m n] = size( graph_temp );
    com_mat = sparse( ii, jj, com(ii) ~= com(jj), m, n );