Finding a critical point in matrix

↘锁芯ラ 提交于 2019-12-23 12:59:20

问题


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

回答1:


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



回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/30471358/finding-a-critical-point-in-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!