I'm pretty new to this myself, so please correct me if I get this wrong.
Using your example, J = ordfilt2(I, 9, true(5))
.
ordfilt2
will move over the 2d array I
in blocks of the same size as true(5)
. For each of these 5x5 blocks, sort all the elements from smallest to largest. Now fill in the corresponding block in J
with a bunch of copies of the 9th smallest element.
To use a smaller example, so I have room to type it:
I = [ 1 2 4 5 ;
5 3 5 1 ;
0 3 5 2 ;
2 1 7 7 ];
J = ordfilt2(I,3, ones(2,2));
Now, let's go through a few blocks one at a time. The first ones(2,2)
block is [1 2; 5 3]
in the top left corner. If we sort these elements, we get [1 2 3 5]
, and since we're looking for the 3rd smallest, we receive a value of 3 for the (1,1) position of J
.
J = [ 3 ? ? ? ;
? ? ? ? ;
? ? ? ? ;
? ? ? ? ];
next up is the I(1:2,2:3)
block. Ordering those elements gives [2 3 4 5]
, so the third smallest is 4. Now we replace J(1,2)
with a 4.
J = [ 3 4 ? ? ;
? ? ? ? ;
? ? ? ? ;
? ? ? ? ];
Go ahead and run this command and compare I, J to understand what's happening. One other thing to know is that the input matrix is padded by default with zeros at the lower and right sides. So the I(4:5,4:5)
block is [ 7 0; 0 0]
.