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

1 visualizzazione (ultimi 30 giorni)
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.

Risposta accettata

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

Più risposte (0)

Categorie

Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by