4

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
statsguyz
  • 419
  • 2
  • 11
  • 35

3 Answers3

3

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
Community
  • 1
  • 1
kkuilla
  • 2,226
  • 3
  • 34
  • 37
2

Try this vectorised 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

Sample Run:

r,c should be a vector of equal length which represents the row and column subs of each Critical point. While val is a vector of 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
Santhan Salai
  • 3,888
  • 19
  • 29
  • 1
    And you can use `[I,J]=ind2sub(size(A),find(C)` with `A(I,J)` to get the row/column indices and value of the critical point. – David May 27 '15 at 01:33
  • @David, so the function [I,J]=ind2sub(size(A),find(C)) would find the row/column indices and the value of the critical points. Where is that stored? In both I and J? – statsguyz May 27 '15 at 01:42
  • When I run my function, critical, I'm not getting the correct results. My first line is "function [r s] = critical(A)". I'm not sure what is wrong – statsguyz May 27 '15 at 01:53
  • Yes that would be great. Again my function is not working. I wonder if my trial version of Matlab is having issues with bsxfun. – statsguyz May 27 '15 at 01:57
  • @statsguyz added comments to clarify.. is your function working now? – Santhan Salai May 27 '15 at 02:26
2

I think there is a simpler way with intersect:

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

 4


col =

 2

UPDATE:

With intersect, in case you have multiple critical points, it will only give you the first one. To have all the indicies, there is also another simple way:

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

Note that the set of critical points now should be the combination of row and col, means you have total 6 critical points in this example: (3,2),(4,2),(3,4),(4,4),(3,6),(4,6).

Here you can find how to export such combination.

Community
  • 1
  • 1
scmg
  • 1,904
  • 1
  • 15
  • 24
  • Fine.. then you have to use `combvec` then `sub2ind` and then `B(ind)` to get the values? – Santhan Salai May 27 '15 at 03:07
  • no, use `meshgrid`: `[p q] = meshgrid(row, col); indicies = [p(:) q(:)];`. It is a built-in function. You need neural network toolbox for `combvec`. – scmg May 27 '15 at 03:11