The attached Distances.xlsxOpens in new tab file contains a spreadsheet with the pairwise distances in miles of the top 100 US cities by population. A preview of the spreadsheet is shown below. The first row and first column contain the city names us

117 views (last 30 days)
The attached Distances.xlsx
Opens in new tab
file contains a spreadsheet with the pairwise distances in miles of the top 100 US cities by population. A preview of the spreadsheet is shown below. The first row and first column contain the city names using the following format: city name comma space capitalized state abbreviation, e.g., Nashville, TN. Note that the very first cell of the spresheet, A1, is blank.
Write a function called get_distance that accepts two character vector inputs representing the names of two cities. The function returns the distance between them as an output argument called distance. For example, the call get_distance('Seattle, WA','Miami, FL') should return 3723. If one or both of the specified cities are not in the file, the function returns -1.
This is a question on coursera. I wrote the following code which gives right answers but the coursera link gives an error that it has an infinite recurssion. please help me find the mistake as I'm new to MATLAB
function distance = get_distance(city1,city2)
[~,city_row]=xlsread('Distances.xlsx',1,'A2:A377');
[~,city_column]=xlsread('Distances.xlsx',1,'B1:LY1');
[~,~,data]= xlsread('Distances.xlsx');
for x=1:length(city_row)
if strcmp(city1,city_row{x,1})
r = x;
break;
else
r=0;
end
end
for y=1:length(city_column)
if strcmp(city2,city_column{1,y})
c = y ;
break;
else
c=0;
end
end
if r==0||c==0
distance =-1;
return;
else
distance=cell2mat(data(r+1,c+1));
end
  4 Comments

Sign in to comment.

Answers (2)

Arafat Hossain
Arafat Hossain on 19 Apr 2020
%try this code hope you will find it usefull.
function distance = get_distance(x,y)
[~,~,raw] = xlsread('Distances.xlsx');
[a b]=size(raw);q=0;w=0;
for i = 2:a
if strcmp(raw{i,1},x)==1
q=i;
end
end
for j = 2:b
if strcmp(raw{1,j},y)==1
w=j;
end
end
if q>1 && w>1
distance = raw{q,w};
else
distance = -1;
end
  7 Comments

Sign in to comment.


Abdulhameed Araromi
Abdulhameed Araromi on 3 Aug 2020
Edited: Abdulhameed Araromi on 5 Aug 2020
You can use this vectorized version. It is short and should be more faster and I hope it help.
function distance = get_distance(city1, city2)
[~,~,raw] = xlsread('Distances.xlsx');
index_list_1 = strcmp(city1,raw(:,1));
index_list_2 = strcmp(city2,raw(1,:));
if all(index_list_1 == 0) || all(index_list_2 == 0)
distance = -1; else
distance = raw{(index_list_1 == 1), (index_list_2 == 1)};
end
  3 Comments
Luis Fernando Torres Barajas
Oh Mr. Abdulhameed, could you please be so gentile and explain what did you just coded, is awosomoe how short you coded it and I don't understand it at all.
Abdulhameed Araromi
Abdulhameed Araromi on 31 Aug 2020
The first line after the function read the distance file into a cell in MATLAB. The second line compare the first input by the user with all the element in the first column of the cell and store it as a Boolean 0 or 1 matrix index_list_1 and the same is done for the first column of the cell which is stored in index_list_2. It compare the input with the first row and first column because that is where the name of the city are. The if statement check if all the result of index-list_1 is equal to zero since if that is true, it means city1 is not present and also check for the result of index_list_two, if any of that is true it returns -1 and the last line check which of the index in the two index_list is equal to one and use the index in raw{} to get the distance between the two cities. E.g. raw{5,6}.

Sign in to comment.

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!