Why is my for loop outputting empty matrices?

조회 수: 3 (최근 30일)
Atiqah Zakirah
Atiqah Zakirah 2017년 5월 30일
댓글: Rik 2017년 5월 31일
This is a snippet of my current code to calculate the number of data points in a single grid. "i = [103.6:(1/75):104]" represents the x axis values of each vertical grid line, "j = [1.25:(1/120):1.5]" represents the y axis values of each horizontal grid line. "coordinates" is a n x 3 numeric matrix that contains the station number, lat coordinates, and lon coordinates. I'm trying to find the row number of all the stations that fall in a single grid. Is there something wrong with my code? It keeps producing empty matrices for "index" and "stns". Safe to say it's not producing anything at all.
Also, in my find function, does (i + 1) represent the next value in the i vector or am I adding a value of 1 to the current value of i? I'm new to Matlab so I apologize if my questions come off as a bit silly. Thanks in advance!
lat = coordinates(:,2); % x coordinates
lon = coordinates(:,3); % y coordinates
% create nested for loop
for i = [103.6:(1/75):104]
for j = [1.25:(1/120):1.5]
% index = row number
index = find(lat > i & lat < i + 1 & lon > j & lon < j + 1);
stns = coordinates(index,1); % stn number
end
end
  댓글 수: 1
Torsten
Torsten 2017년 5월 30일
편집: Torsten 2017년 5월 30일
I'm quite sure you won't find indices where (lat>i & lat<i+1) and (lon>j & lon<j+1) are simultaneously satisfied .
You will first have to apply "meshgrid" to "lat" and "lon" and then start your search.
Best wishes
Torsten.

댓글을 달려면 로그인하십시오.

채택된 답변

Rik
Rik 2017년 5월 30일
You are adding 1 to i, because Matlab has no way of knowing that you mean the next value.
In the find command, you should really add some parentheses to make sure that you are actually getting what you mean.
You are overwriting stns each loop. What I would do if I were you is making stns a cell.
%generate dummy data:
n=1000;
coordinates=[randperm(n)', rand(n,1)*0.4+103.6, rand(n,1)*0.25+1.25];
lat = coordinates(:,2); % x coordinates
lon = coordinates(:,3); % y coordinates
% create nested for loop
i_vector=103.6:(1/75):104;
j_vector=1.25:(1/120):1.5;
stns=cell(length(i_vector),length(j_vector));
for i = 1:(length(i_vector)-1)
%you can't access element end+1
for j = 1:(length(j_vector)-1)
% index = logical indexing vector
index = (lat > i_vector(i)) ...
& (lat < i_vector(i+1)) ...
& (lon > j_vector(j)) ...
& (lon < j_vector(j+1)) ;
stns{i,j} = coordinates(index,1); % stn number
end
end
Now you can look in the variable editor to see what stations are in what grid location. If you want to have the row number you can add this line coordinates(:,4)=1:size(coordinates,1); and change the ,1 to ,4 in the line that writes to stns.
PS I think this could be done more elegantly (and faster for bigger datasets), but this is most similar to your code, easy to understand, and not absurdly slow for most lists of stations.
  댓글 수: 2
Atiqah Zakirah
Atiqah Zakirah 2017년 5월 31일
Code works great! Thank you, Rik!
Rik
Rik 2017년 5월 31일
You're welcome, although I advise you to also have look at histcounts2, as Jan Simon suggested. Depending on how you want to further process the result that may be more efficient.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2017년 5월 30일
This is a job for the efficient histcounts2.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by