0

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!

jdbul33
  • 165
  • 1
  • 8
  • 2
    Can you provide a minimal example of what you're trying to obtain. – obchardon Mar 21 '18 at 14:19
  • Sorry, I am on my phone here at work. But here is the idea: [1;0;1] × [3 3 2; 5 4 1; 8 2 2] will give me [3 3 2; 0 0 0; 8 2 2] – jdbul33 Mar 21 '18 at 15:49
  • 1
    so are you simply looking for `x.'.*y` or `x.*y` ? (where .`.*` represent the element wise multiplication) – obchardon Mar 21 '18 at 15:59

1 Answers1

2

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
PTRK
  • 899
  • 1
  • 5
  • 18
  • 2
    Instead of `repmat`, prefer `bsxfun`: `bsxfun(@times,vect,mat)`. In newer version of MATLAB, and in Octave, you can simply do `vect.*mat`, there is implicit expansion of singleton dimensions. – Cris Luengo Mar 21 '18 at 17:28
  • I didn't know that. Reading [mathworks](https://fr.mathworks.com/help/matlab/ref/bsxfun.html?s_tid=gn_loc_drop), this is true for version >= 2016b. – PTRK Mar 21 '18 at 19:40
  • I always forget which versions introduce new features, so I just say "newer versions" and try to get away with that. :p – Cris Luengo Mar 21 '18 at 19:56