寻找矩阵中的临界点

4

我正在尝试找到矩阵中的关键点。在索引(i,j)处的值应大于或等于其行中的所有元素,并小于或等于其列中的所有元素。

这是我目前的代码(有误但接近正确):

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

你的 A 矩阵可以有多大? - Autonomous
我的意思是它可能非常大。这会成为问题吗? - statsguyz
3个回答

3
您可以使用逻辑索引。
minI = min(A,[],1);
maxI = max(A,[],2);
[row,col] = find(((A.'==maxI.').' & A==minI) ==1)

详情

记得Matlab是列主序的。因此,我们需要转置A和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

2

尝试使用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

示例运行:

rc应该是一个相等长度的向量,表示每个关键点的行和列。而val是一个相同长度的向量,给出了关键点本身的值。

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

1
你可以使用 [I,J]=ind2sub(size(A),find(C) 以及 A(I,J) 来获取临界点的行/列索引和值。 - David
@David,所以函数[I,J]=ind2sub(size(A),find(C))将找到临界点的行/列索引和值。它存储在哪里?在I和J中都有吗? - statsguyz
当我运行我的函数critical时,我没有得到正确的结果。我的第一行是“function [r s] = critical(A)”。我不确定哪里出了问题。 - statsguyz
好的,那太好了。我的函数还是不起作用。我想知道我的Matlab试用版是否与bsxfun有问题。 - statsguyz
@statsguyz添加了注释以澄清..你的函数现在工作了吗? - Santhan Salai

2

我认为使用 intersect 有一个更简单的方法:

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

 4


col =

 2

更新:

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

请注意,关键点集现在应该是rowcol的组合,这意味着在此示例中您总共有6个关键点:(3,2),(4,2),(3,4),(4,4),(3,6),(4,6)。您可以在这里找到如何导出这种组合的方法。

好的,那么你需要使用combvec,然后使用sub2ind,最后使用B(ind)来获取值吗? - Santhan Salai
不,不使用meshgrid: [p q] = meshgrid(row, col); indicies = [p(:) q(:)];。这是一个内置函数。您需要神经网络工具箱来使用combvec - scmg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接