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 );