Find indices of the lowest neighbour in matrix

2 次查看(过去 30 天)
I'm trying to find the indices of the lowest value of three neighbours (red in picture below) for backtracking a path in a matrix
i = 331
j = end
[x y] = min([cost(end-1,j-1) cost(end,j-1) cost(end-1,j)])
Screenshot 2020-01-29 at 11.48.50.png
Can anyone help me with this?
thanks

回答(1 个)

Vinai Datta Thatiparthi
Hello Morten,
Use a combination of find and min functions to get the output -
matVal = [ .. ]; % Enter your input matrix here
% Enter the coordinates for the value of which you want to find the least neighbour of
xCoOrd = .. ;
yCoOrd = .. ;
% Find out all the neighbours of the element that you picked
convOut = conv2(double(I==I(xCoOrd, yCoOrd)), ones(3,3), 'same');
idx = find(convOut - double(I==I(xCoOrd, yCoOrd)));
% The index of the least neighbour
leastNeighbourIdx = find(I == min(I(idx)));
Use ind2sub function on the variable leastNeighbourIdx to get the answer in subscripts.
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by