在数组的最大值附近查找数组

bei*_*ndo 2 matlab image-processing matrix

我在这里正在处理一个非常复杂的问题。用语言来描述是非常困难的,所以我将尝试用一个例子来解释它。

假设我有一个值矩阵:

A = 

[31 85 36 71 51] 
[12 33 74 39 12]
[67 11 13 14 18]
[35 36 84 33 57]
Run Code Online (Sandbox Code Playgroud)

现在,我想首先在第一维中找到一个最大向量,这很容易:

[max_vector,~] = max(A,[],1);


max_vector=[67,85, 84, 71,57]
Run Code Online (Sandbox Code Playgroud)

现在,我想获得一个“细长”矩阵,其值围绕最大值(定期索引):

Desired_Matrix =

 [12 36 36 33 18]
 [67 85 84 71 57]
 [35 33 13 39 51]
Run Code Online (Sandbox Code Playgroud)

这是矩阵,其向量围绕矩阵A的最大值。有人可以告诉我如何在不使用double for循环的情况下执行此操作吗?

谢谢!

Han*_*rse 5

% Input.
A = [31 85 36 71 51; 12 33 74 39 12; 67 11 13 14 18; 35 36 84 33 57]

% Dimensions needed.
nRows = size(A, 1);
nCols = size(A, 2);

% Get maxima and corresponding indices in input.
[max_vector, ind] = max(A);

% Get neighbouring indices.
ind = [ind - 1; ind; ind + 1];

% Modulo indices to prevent dimension overflow.
ind = mod(ind, nRows);

% Correct zero indices.
ind(ind == 0) = nRows;

% Calculate correct indices in A.
temp = repmat(0:nRows:nRows*(nCols-1), 3, 1);
ind = ind + temp;

% Output.
B = A(ind)
Run Code Online (Sandbox Code Playgroud)

因为我们每列最大指数,但后来想原始数组中访问这些内容A,我们需要适当的线性指标A。在这里,技巧是将行数乘以列索引(从0开始)。最简单的理解方法可能是删除分号,并检查的中间值ind