# 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

89 visualizzazioni (ultimi 30 giorni)
AKRITI KOTHIALA il 13 Gen 2020
Commentato: Mekaideche il 21 Lug 2023
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)
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 CommentiMostra 2 commenti meno recentiNascondi 2 commenti meno recenti
John Angelo il 23 Giu 2023
thank you so much!
Mekaideche il 21 Lug 2023
can u explane to me how u know the length of the table ,the didn't say that.and the line 25?

Accedi per commentare.

### Risposte (2)

Arafat Hossain il 19 Apr 2020
%try this code hope you will find it usefull.
function distance = get_distance(x,y)
[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 CommentiMostra 5 commenti meno recentiNascondi 5 commenti meno recenti
ekaterini adburrahmani il 8 Feb 2021
it's very difficult can anyone help me
Danish il 22 Ott 2022
it is helpful bro thank you.

Accedi per commentare.

Abdulhameed Araromi il 3 Ago 2020
Modificato: Abdulhameed Araromi il 5 Ago 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)
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 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
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 il 31 Ago 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}.

Accedi per commentare.

### Categorie

Scopri di più su MATLAB in Help Center e File Exchange

R2019b

### Community Treasure Hunt

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

Start Hunting!

Translated by