在矩阵中找到一个临界点 [英] Finding a critical point in matrix
本文介绍了在矩阵中找到一个临界点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我所拥有的(它已关闭但我已经接近):
function C = critical(A)
[nrow ncol] = size(A);
C = []; (A(i,j)> = A(i,1:end))&& amp;& amp;& (A(i,j)≤A(1:end,j))
C = [C; A(i,j)]
end
end
end
解决方案
使用 bsxfun
函数[r,c,criP] = critical(A)
%//找到每个col的最小值和最大值。 row resptly
minI = min(A,[],1);
maxI = max(A,[],2);
%//匹配min&最大为每个col和行resptly
%//得到元素的索引满足条件
idx = find(bsxfun(@ eq,A,maxI)& bsxfun(@ eq,A,微型));
%//从索引中获取相应的值
criP = A(idx);
%//同样得到相应的行和col sub
[r,c] = ind2sub(size(A),idx);
end
运行示例
r
, c
应该是等长的向量,每个关键点的列子目录。虽然 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] =危急(A)
r =
4
5
c =
2
2
val =
3
3
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
解决方案
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
这篇关于在矩阵中找到一个临界点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文