Compute distance between between points stored vectors of different lengths whilst fixing elements

4 visualizzazioni (ultimi 30 giorni)
Hi everyone,
I am currently stuck with this problem and it's driving me nuts that I can't work it out. Therefore, I would greatly appreciate any help I could get.
There are four vectors which contain coordinates (longitdue and latitude values).
  1. Y1_coord.Y (latitude) is a 1×1181 cell array
  2. X1_coord.X (longitude) is a 1×1181 cell array
  3. Y2_coord (latitude) this is an integer vector of length 25,000
  4. X2_coord (longitude) this is an integer vector of length 25,000
I simply want to compute the distance between coordinates via a matlab "distance" function via:
arclen = distance([Y1_coord, X1_coord], [Y2_coord, X2_coord ]) ;
Here comes the problem:
When I go into the Y1 and X1 cell arrays and look at the rows, then the number of entries varies for each row. For example:
Y1_coord(1).Y is of size 1 x 260 entries
X1_coord(1).X is of size 1 x 260 entries
Y1_coord(2).Y is of size 1 x 1260 entries
X1_coord(2).X is of size 1 x 1260 entries
...
Y1_coord(1181).X is of size 1 x 2149 entries
X1_coord(1181).X is of size 1 x 2149 entries
What I want to do is, is to fix the first entry of Y1 and X1 (i.e. Y1_coord(1).Y(1) and X1_coord(1).X(1)) and compute the distance for each entry of Y2 and X2. Once this is computed, fix the second entry of Y1 and X1 (i.e. Y1_coord(1).Y(2) and X1_coord(1).X(2)) and compute the distance for each entry of Y2 and X2. Do this until the last entry (e.g. 260), after this jump into the next row and repeat the above steps until we reach Y1_coord(1181).Y(2149) and X1_coord(1181).X(2149)
I tried to solve this with a for loop but without any success. Also let me know if the above description wasn't clear. Many thanks in advance.
Rob

Risposta accettata

William Rose
William Rose il 2 Feb 2022
Here's an example. Location1 = Y1,X1 = cell arrays with unequal size cells: 2 locations near Bangor ME, 3 near LA, 4 near Miami. Location2 = Y2,X2 = 3 locations near Honolulu. The 27 distances from the nine Location1s to the three Location2s are computed.
%distanceTable.m
%For Rob, Matlab Answers, 20220201
%W. Rose, 20220201
clear
%create location data
X1=cell(3,1); %allocate cell array
Y1=cell(3,1); %allocate cell array
X1{1}=-69+rand(1,2); %Bangor Maine: 45N, 69W
Y1{1}=45+rand(1,2); %Bangor Maine: 45N, 69W
X1{2}=-118+rand(1,3); %Los Angeles: 34N, 118W
Y1{2}=34+rand(1,3); %Los Angeles: 34N, 118W
X1{3}=-80+rand(1,4); %Miami: 26N, 80W
Y1{3}=26+rand(1,4); %Miami: 26N, 80W
X2=-158+rand(1,3); %Honolulu: 21N, 158W
Y2=21+rand(1,3); %Honolulu: 21N, 158W
%Find distances from locations in X1,Y1 to locations in X2,Y2
for i=1:length(X1)
for j=1:length(X1{i})
for k=1:length(X2)
fprintf('Distance from Location1(%d,%d) to Location2(%d)=%.2f\n',...
i,j,k,distance([Y1{i}(j),X1{i}(j)],[Y2(k),X2(k)]));
end
end
end
Distance from Location1(1,1) to Location2(1)=74.19 Distance from Location1(1,1) to Location2(2)=74.27 Distance from Location1(1,1) to Location2(3)=74.22 Distance from Location1(1,2) to Location2(1)=73.91 Distance from Location1(1,2) to Location2(2)=73.99 Distance from Location1(1,2) to Location2(3)=73.93 Distance from Location1(2,1) to Location2(1)=36.94 Distance from Location1(2,1) to Location2(2)=37.15 Distance from Location1(2,1) to Location2(3)=36.93 Distance from Location1(2,2) to Location2(1)=36.81 Distance from Location1(2,2) to Location2(2)=37.02 Distance from Location1(2,2) to Location2(3)=36.80 Distance from Location1(2,3) to Location2(1)=36.87 Distance from Location1(2,3) to Location2(2)=37.07 Distance from Location1(2,3) to Location2(3)=36.86 Distance from Location1(3,1) to Location2(1)=69.82 Distance from Location1(3,1) to Location2(2)=70.09 Distance from Location1(3,1) to Location2(3)=69.80 Distance from Location1(3,2) to Location2(1)=70.14 Distance from Location1(3,2) to Location2(2)=70.41 Distance from Location1(3,2) to Location2(3)=70.12 Distance from Location1(3,3) to Location2(1)=69.55 Distance from Location1(3,3) to Location2(2)=69.82 Distance from Location1(3,3) to Location2(3)=69.53 Distance from Location1(3,4) to Location2(1)=70.08 Distance from Location1(3,4) to Location2(2)=70.35 Distance from Location1(3,4) to Location2(3)=70.06
How's that?
  4 Commenti
Rob
Rob il 4 Feb 2022
I know.. Im actually trying to look for a more efficienct way to compute this. Another problem is storage..
William Rose
William Rose il 5 Feb 2022
@Rob, Good luck with the speed and the storage. Is this something meteorological? Your integer lats and longs make me think of a 1x1 degree grid covering the Earth - 64,082 points, not 25,000.

Accedi per commentare.

Più risposte (1)

William Rose
William Rose il 2 Feb 2022
My understading is that X1,Y1 contains coordiates for 1 to 2 million locations (1181 rows times several hundred up to over two thousand locations per row). You wish to compute the distances from these 1 to 2 million locations to a second set of 25000 locations, which are in X2,Y2. This will yield 25 billion to 50 billion distances. Is that correct?
Please save the four vectors (actually two cell arrays and two vectors) in a .mat file, and attach the .ma file to your reply, so that we can open it. Or post a shortened versions that captures th challenges, csuch as cells with unequal numbers of elements.
You say X2 and Y2 are integer vectors. That suprises me since it suggests that the latitudes and longitudes have been rounded to the nearest whole numbers. Why would you do that?
When I learned celestial navigaiton in the pre-electronic calculator days, one rounded the meridian angle to an integer number of degrees, so that you could look up angles in H.O. 249 to solve the spherical triangle problem.
  1 Commento
Rob
Rob il 2 Feb 2022
Hi Will, thanks for your help. Regarding your first paragraph that's unfortunately true. Let me put things in a mat file to clarify things. Thank Rob

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements 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