DISTANCE BETWEEN ANY TWO PLACES
Show older comments
Hey guys,
Im pretty new to matlab and am having quite some trouble with a question iv been given at uni. The question wants me to create a user friendly interface which allows users to calculate the distance between any two cities in australia.
I found a link online about how to do this by entering the co ordinates (>distdim(distance(53.9, 27.5667, 10.5207, -66.9245),'deg','kilometers') however this doesn't really help me as it isnt very user friendly to require the user to type in his own co ordinates.
If someone could please help me with this it would be greatly appreciated.
1 Comment
Image Analyst
on 11 Oct 2012
Do you have the mapping toolbox? (If so, add it to the Products below.)
Answers (1)
Andrei Bobrov
on 12 Oct 2012
Edited: Andrei Bobrov
on 23 Oct 2012
use Mapping Toolbox
Create data with coordinates of citys of Australia.
eg
global coord_citys_aus;
coord_citys_aus = {'Adelaida', -34.899448, 138.589783
'Brisbane', -27.452228, 153.039551
'Canberra', -35.308401, 149.124298
'Perth', -31.932351, 115.861816
'Darwin', -12.462789, 130.841739}
save('coord_citys_aus.mat','coord_citys_aus');
% Your function
function out = myauscitysdist(namefirstcity,namesecondcity)
global coord_citys_aus;
indat = coord_citys_aus(ismember(coord_citys_aus(:,1),...
{namefirstcity,namesecondcity}),2:3)';
out = deg2km(distance(indat{:}));
% Example use
>> load coord_citys_aus.mat;
>> out = myauscitysdist('Perth','Brisbane')
out =
3608
variant without use function distance from Mapping Toolbox
function out = distbetweencitysAustrig0(namescity)
global citys_Aus;
[~,i0] = ismember(namescity,citys_Aus(:,1));
jj = nchoosek(1:numel(namescity),2);
d = cell2mat(reshape(citys_Aus(i0(jj),2),[],1,2));
p = cosd(d(:,1,:));
x2 = sum(p.^2,3) - 2*prod(p,3).*cosd(diff(d(:,2,:),1,3));
y2 = diff(sind(d(:,1,:)),1,3).^2;
out = 12742*asin(sqrt(x2 + y2)/2);
end
and example use function distbetweencitysAustrig0
>> global citys_Aus;
load('citys_Aus.mat');
>> namescity = {'Adelaide';'Darwin';'Sydney';};
>> out = distbetweencitysAustrig0(namescity)
out =
2617.6
1161.9
3148.6
Categories
Find more on Mathematics and Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!