Condition for columns based on the same index as the vector

I'm trying to get a logical matrix as a result of the conditions that are specific to each column M(:,i)of the original matrix, based on the value of the same index iin the vector N, that is, N(i).

I looked it online, but I can not find anything like it. There should be a simple and clean way to do this.

M =

     3    -1   100     8
   200     2   300     4
   -10     0     0   400

N =

     4     0    90     7

and my sought-after solution for each column M(:,i)is less than N(i):

     1     1     0     0
     0     0     0     1
     1     0     1     0
+4
source share
2 answers

This is a standard use case for bsxfun:

O = bsxfun(@lt, M, N)

@lt "", .. <. bsxfun "" N , @lt M N.

, , for -loop:

O = zeros(size(M));
for row = 1:size(M,1)
    O(row,:) = M(row,:) < N;
end

repmat:

O = M < repmat(N, size(M,1), 1);

MATLAB bsxfun .

+5

arrayfun :

T = arrayfun(@(jj)M(:,jj) < N(jj), 1:numel(N), 'UniformOutput', false);
result = cat(2,T{:});

: , bsxfun .

+3

Source: https://habr.com/ru/post/1615366/


All Articles