Compare matrix column-wise with vector
Show older comments
Dear all,
I have an mxn matrix and a 1xn vector I want to compare each ith column of the matrix with the ith value of the vector. The result should be an mxn logical matrix to be used
Example:
minVec = [ 0 2 0];
maxVec = [10 10 9];
mat = [-1 2 5;
9 15 3];
I would like to calculate the following, but without a for-loop...
mask(:,i) = mat(:,i)<minVec(i) | mat(:,i)>maxVec(i);
mask = [ 1 0 0;
0 1 0]
I guess there must be some solution using arrayfun, perhaps?
Any help is greatly appreciated!
Thank you very much, Philip
Accepted Answer
More Answers (1)
Image Analyst
on 22 Nov 2013
Try this:
[rows, columns] = size(mat)
mask = mat < repmat(minVec, [rows, 1]) | mat > repmat(maxVec, [rows, 1])
1 Comment
Philip Ohnewein
on 22 Nov 2013
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!