Extract whole line from array if one value is amongst the x smallest

1 view (last 30 days)
I'm calculating the distance between one point and several other points (stored in an array, let's call it storageArray(index,<several more indices>, x1, x2, x3). Of the hundreds of points in my array, I only calculate the distances for a few dozens at a time. I then store the calculated distances and their index in another array (distanceArray(index, distance)). This works fine.
I then want to copy the storageArray's lines of the points that yield the x smallest distances (usually 3-5). How do I go about that?
On my search for a solution found a few that I could use to determine the x smallest from an array but I can't seem to find a solution that keeps the indices (which I'd need to copy the right lines) of those x smallest elements. I'm sure I'm probably just missing something obvious and am probably thinking too complicated about it.

Accepted Answer

Walter Roberson
Walter Roberson on 5 Feb 2014
[sortedvals, sortidx] = sort(YourValues(:,1));
YourValues(sortidx(1),:)
YourValues(sortidx(2),:)

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!