How to find elements of an Array using indexes.

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 件のコメント

James Tursa
James Tursa 2018 年 7 月 5 日
Indexes can't be 0 in MATLAB. Can you give a small example? Normally you would just do something like this:
result = my_array(I,:);
Brandon Bush
Brandon Bush 2018 年 7 月 5 日
SAT_RNFL = nan(31,1); %Creates a 31 x 1 array
for i = 1:31
[Y, I] = min((latS - latG(i)).^2 + (lonS - lonG(i)).^2); %Finds matching Lat and Lon values
disp(I) %Prints indices of the matched coordinates
SAT_RNFL(i) = RNFLS(I,1); %Sticks the satellite rainfall value in the new array
end
This is the code I use to match Lat and Lon coordinates of rain gauge and satellite precip values. The Satellite coordinates are in the 388x2 array and [I] is the indices of those values that got matched with the coordinates in the rain gauge array, which is 31x2. I need to print out the actual coordinates that got matched instead of the indices.
Brandon Bush
Brandon Bush 2018 年 7 月 5 日
The SAT_RNFL and RNFLS arrays are just rainfall totals and have nothing to do with the coordinates
Brandon Bush
Brandon Bush 2018 年 7 月 5 日
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

サインインしてコメントする。

 採用された回答

dpb
dpb 2018 年 7 月 5 日
編集済み: dpb 2018 年 7 月 8 日

0 投票

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 件のコメント

Brandon Bush
Brandon Bush 2018 年 7 月 5 日
It didnt quite work. It gave me indices that didnt coordinate with those represented by I
dpb
dpb 2018 年 7 月 8 日
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...

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2018 年 7 月 5 日

編集済み:

dpb
2018 年 7 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by