Compare matrix column-wise with vector
Afficher commentaires plus anciens
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
Réponse acceptée
Plus de réponses (1)
Image Analyst
le 22 Nov 2013
Try this:
[rows, columns] = size(mat)
mask = mat < repmat(minVec, [rows, 1]) | mat > repmat(maxVec, [rows, 1])
1 commentaire
Philip Ohnewein
le 22 Nov 2013
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!