Search for a critical point in a matrix

I am trying to find a critical point in the matrix. The value in the index (i, j) must be greater than or equal to all the elements in its row and less than or equal to all the elements in its column.

Here's what I have (this, 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
+4
source share
3 answers

Try this vector solution using bsxfun

function [ r,c,criP ] = critical( A )

    %// finding the min and max values of each col & row resptly
    minI = min(A,[],1);
    maxI = max(A,[],2);

    %// matching all the values of min & max for each col and row resptly 
    %// getting the indexes of the elements satisfying both the conditions
    idx = find(bsxfun(@eq,A,maxI) & bsxfun(@eq,A,minI));

    %// getting the corresponding values from the indexes
    criP = A(idx);

    %// Also getting corresponding row and col sub
    [r,c] = ind2sub(size(A),idx);
end

Run Example:

r, cMust be equal to the length of a vector which represents the essence of rows and columns of each critical point. Although val- a vector of the same length giving the value of the critical point itself

>> A

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


>> [r,c,val] = critical(A)

r =

 4
 5

c =

 2
 2

val =

 3
 3
+2
source

You can use logical indexing.

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

More details

Remember that Matlab is the main column. Therefore, we transfer 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

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    

((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

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

row =

   4
   5

col =

   2
   2
+3

, intersect:

>> [~, row, col] = intersect(max(A,[],2), min(A));
row =

 4


col =

 2

UPDATE:

intersect, , . , :

>> B

B =

 3     4     1     4     2     5
 2     5     2     4     4     4
 4     4     2     4     2     4
 3     4     1     4     1     4
 2     5     4     4     4     5

>> row = find(ismember(max(B,[],2),min(B)))

row =

 3
 4

>> col = find(ismember(min(B),max(B,[],2)))

col =

 2     4     6

, row col, , 6 : (3,2),(4,2),(3,4),(4,4),(3,6),(4,6).

, ​​.

+2

All Articles