How to find elements of an Array using indexes.
조회 수: 1 (최근 30일)
이전 댓글 표시
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.
채택된 답변
dpb
2018년 7월 5일
편집: dpb
2018년 7월 8일
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
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개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!