Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding a critical point in matrix

I'm attempting to find a critical point in a matrix. The value at index (i,j) should be greater than or equal to all elements in its row, and less than or equal to all elements in its column.

Here is what I have (it's off but I'm close):

function C = critical(A)
[nrow ncol] = size(A);
C = [];
for i = 1:nrow
    for j = 1:ncol
        if (A(i,j) >= A(i,1:end)) && (A(i,j) <= A(1:end,j))
            C = [C ; A(i,j)]
        end
    end
end
like image 660
statsguyz Avatar asked Mar 16 '23 22:03

statsguyz


1 Answers

You can use logical indexing.

minI = min(A,[],1);
maxI = max(A,[],2);
[row,col] = find(((A.'==maxI.').' & A==minI) ==1)

Details

Remember that Matlab is column major. We therefore transpose A and maxI.

A = [

   3   4   1   1   2
   2   4   2   1   4
   4   3   2   1   2
   3   3   1   1   1
   2   3   0   2   1];

A.'==maxI.'
ans =

   0   0   1   1   0
   1   1   0   1   1
   0   0   0   0   0
   0   0   0   0   0
   0   1   0   0   0

Then do the minimum

A==minI
ans =

   0   0   0   1   0
   1   0   0   1   0
   0   1   0   1   0
   0   1   0   1   1
   1   1   1   0   1    

And then multiply the two

((A.'==maxI.').' & A==minI)
ans =

   0   0   0   0   0
   0   0   0   0   0
   0   0   0   0   0
   0   1   0   0   0
   0   1   0   0   0

Then find the rows and cols

[row,col] = find(((A.'==maxI.').' & A==minI) ==1)

row =

   4
   5

col =

   2
   2
like image 56
kkuilla Avatar answered Mar 24 '23 10:03

kkuilla