Search code examples
matlablogiccomparisonbsxfunboolean-algebra

Compare two matrices of unequal size


I have two matrices A(10,5) and B(30,5). By comparison the final output required is logical array of size (10,30) or (30,10). My code:

A=rand(10,5)
B=rand(30,5)
for i=1:size(A,1)
      X(:,i)=all(bsxfun(@le,A(i,:),B))
end

In my code, I am not getting the desired result, in terms of size of output matrix.


Solution

  • Is this what you want?

    X = all(bsxfun(@le, permute(A, [1 3 2]), permute(B, [3 1 2])), 3);
    

    Using the above code, X(m,n) will be true iff each entry in A(m,:) is less than or equal to the corresponding entry in B(n,:).