matlaboctave

Matrix Row size comparison with Vector item?


I have matrix [m x n] and vector [m], and I want to compare each row with the corresponding vector number, is there way to do this vectorized method ?


Solution

  • Use bsxfun:

    % example data
    M = rand(5, 3);
    V = rand(5,1);
    
    % for equality (==) : 
    bsxfun(@eq, V, M);
    
    % for greater-than (>) : 
    bsxfun(@gt, V, M);
    
    % for greater-than-or-equals (>=) : 
    bsxfun(@ge, V, M);
    

    etc. The list of available functions is listed in help bsxfun.