Filter löschen
Filter löschen

How to find elements of an Array using indexes.

1 Ansicht (letzte 30 Tage)
Brandon Bush
Brandon Bush am 5 Jul. 2018
Bearbeitet: dpb am 8 Jul. 2018
I need to find the elements in a 388x2 array by using indexes given by [I]. The indexes can be any number within the 0-388 range and there are 31 total.
  4 Kommentare
Brandon Bush
Brandon Bush am 5 Jul. 2018
The SAT_RNFL and RNFLS arrays are just rainfall totals and have nothing to do with the coordinates
Brandon Bush
Brandon Bush am 5 Jul. 2018
Also when the loop finishes, the only thing that gets mapped to I is the last value, however, when I use disp(I) it prints out all the indices

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

dpb
dpb am 5 Jul. 2018
Bearbeitet: dpb am 8 Jul. 2018
for i = 1:31
[Y, I] = min((latS-latG(i)).^2 + (lonS-lonG(i)).^2);
disp(I)
...
" %Prints indices of the matched coordinates"
But this is only sequentially through the loop for each element in turn, you haven't saved the found location; each pass overwrites the previous so indeed when the loop finishes all you have is the last iteration.
The most "deadahead" solution given the existing code would be
N=31;
Y=zeros(N,1); I=Y; % preallocate
for i = 1:N
[Y(i), I(i)] = min((latS-latG(i)).^2 + (lonS-lonG(i)).^2);
...
  2 Kommentare
Brandon Bush
Brandon Bush am 5 Jul. 2018
It didnt quite work. It gave me indices that didnt coordinate with those represented by I
dpb
dpb am 8 Jul. 2018
I guess I misinterpreted what you were asking for...I thought the I was a "pick 'em" vector to select a subset, not a predefined set of indices. Your problem is outlined in revised Answer...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by