Search code examples
matlabgraphdigraphs

Matrix multiplication MATLAB


So maybe I am overthinking this and making a mess of it....

I have a digraph in MATLAB. I need to change it to a undirected graph to evaluate it with minimum spanning tree (right? It won't work on digraph). I have a nx1 matrix of binaries (1 is unique, 0 is repeat) that denote repeats, and my node-node-edgeweight matrix is in the form nx3. It seems my directed edges are the same either directions, so changing it to undirected shouldn't make a difference.

How can I use the column vector of binaries to zero out all three columns of repeats in my main matrix, so it will only show undirected edges?

Also, if there is another approach I am missing, I would love that!


Solution

  • From your example:

    vect = [1;0;1]; % n x 1
    mat  = [3 3 2; 5 4 1; 8 2 2]; % n x p
    

    First idea

    out = repmat(vect,1,size(mat,2)).*mat; 
    

    Second idea

    out = mat;
    out(find(~vect),:) = 0;
    

    For MATLAB >= r2007a

    (from Chris Luengo comment)

    out = bsxfun(@times,vect,mat)
    

    For MATLAB >= r2016b

    (from Chris Luengo comment)

    out = mat.*vect