Elementwise comparison

32 Ansichten (letzte 30 Tage)
Brian
Brian am 5 Apr. 2012
Hi, I have a matrix that I would like to filter the row results using a different filter for each row without having to write a for statement. Take the following example.
z = [1 2 3;2 1 3];
y = [2;1];
I'd like to be able to say, count the values by row from z where each specific value is compared to the row value from y. I would expect the following to yield a 2x3 logical that I could perform my sum function on.
x = z .> y;
Would yield
x = logical([0 0 1;1 0 1]);
And
sum(x,2) = [1;2]
Thanks a lot, Brian

Akzeptierte Antwort

Oleg Komarov
Oleg Komarov am 5 Apr. 2012
There's no logical elementwise operator since they already require the array to match dimensions.
Thus, one way is to replicate y to the size of z and then compare. To avoid repmat, you can use directly:
bsxfun(@gt,z,y)
  1 Kommentar
Brian
Brian am 5 Apr. 2012
That's perfect. Thank you.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by