Clear Filters
Clear Filters

Associate indices with matrix row

1 view (last 30 days)
Danilo M
Danilo M on 25 Jul 2018
Edited: Adam Danz on 25 Jul 2018
I need to find nearest gauge stations for a list of points. After calculate the distance from stations to each point, I'm using this code to return the minimum values of distance:
[minValues, minIndices] = min(dist,[],2)
How can I capture the first row of gauge station list for each indice?
i.e. for this matrix minValues = [2; 3; 2; 5] gauge = [12; 23; 34; 45; 56; 67]
it creates anoter like [2 23; 3 34; 2 23; 5 56]

Answers (1)

Adam Danz
Adam Danz on 25 Jul 2018
Edited: Adam Danz on 25 Jul 2018
Your question is unclear but I think I get what you're asking.
I assuming 'dist' is a matrix of distances were columns are gauge stations and rows are 'points' such that dist(i,j) is the distance between point(i) and station(j).
minValues(i) is the minimum distance between point(i) and all of the gauge stations (or the other way around, depending on how your matrix is organized).
minIndices(i) is the column number of 'dist' that contains the minimum for row(i). In other words, minIndices(i) tells you the gauge station that is closest to point(i).
To summarize, minValues(1:5) are the minimum distances for points(1:5) and the nearest gauge stations for those points are minIndices(1:5).
To create a linear index of the 'dist' matrix:
x = sub2ind(size(dist), 1:length(minIndices), minIndices');

Community Treasure Hunt

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

Start Hunting!