Matrix-vector boolean comparison

5 visualizaciones (últimos 30 días)
Greg
Greg el 11 de Oct. de 2011
I might be having a brain fart, but if I'm not...
I have a matrix M (mxn) and a vector V (mx1). On each row of M, I want to see how many elements are greater than the element on the corresponding row of V. That is, I want R, where
for i=1:m;
R(i)=length(find( M(i,:) > V(i)));
end
My question is - can I do this without a loop? Is there a one line operation for this? I know the loop in the example is trivial, but I'm trying to optimize code that takes hours to run and where I run through the loop many many times.

Respuesta aceptada

Sean de Wolski
Sean de Wolski el 11 de Oct. de 2011
Sure:
R = sum(bsxfun(@gt,M,V),2)
Also note, just preallocating R would save you a lot of time for large m.
R = zeros(m,1);
for ii =...
R(ii) = ...
end
  1 comentario
Greg
Greg el 11 de Oct. de 2011
Awesome. Preallocating was done, smarter things weren't. Thanks!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by