# Excel I/o matlab

20 visualizzazioni (ultimi 30 giorni)
Priyamvada Shankar il 24 Mar 2019
Commentato: Lucero il 24 Giu 2022
The attached Distances.xlsx 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. Preview of the first five cities of Distances.xlsx
##### 20 CommentiMostra 18 commenti meno recentiNascondi 18 commenti meno recenti
nicola Braham il 29 Ago 2020
is it just because the first row and first column contain city names not distances?
Walter Roberson il 29 Ago 2020
Neither q nor w are given (not passed in by the user, not prompted for). Both of them are calculated.
Notice this particular part of the assignment:
"If one or both of the specified cities are not in the file, the function returns -1."
Therefore we cannot assume that the cities passed in will definitely be in the file, so we cannot assume that the positions q or w will have been set to valid values. Testing q and w is checking to be sure that the cities were actually found.

Accedi per commentare.

### Risposte (5)

Sravani Kurma il 28 Lug 2020
Modificato: Sravani Kurma il 28 Lug 2020
code with explaination:
function distance=get_distance(alpha,beta)
[m,n]=size(d);% to find the size of d interms of matrix dimensions mxn
s=strcmp(alpha,d(1,1:n));% comparing each string of one row of sheet with given name of the city say alpha
p=(sum(s==1));% aftr comparing output will be logical 1 or 0, so, we get max one logical 1 value and all other zeros ,so sum will be 1+n(0)=1
if p==0 %if p==0,no matching city,,,
distance= -1;
return
end
t=strcmp(beta,d(1,1:m));
q=(sum(t==1));
if q==0
distance= -1;
return
end
distance=d{find(s==1),find(t==1)};%find will given the index value of t==1 and s==1
##### 4 CommentiMostra 2 commenti meno recentiNascondi 2 commenti meno recenti
Rik il 28 Lug 2020
There is nothing wrong with the code, but it is not optimal or intuitive, and therefore not perfect. Compare the two blocks below. For positive integer values of b they will have the same result, but one is clearly better.
a+b
for n=1:b
a=a+1;
end
Sravani Kurma il 28 Lug 2020
Ok..tq

Accedi per commentare.

Irfan Hussain il 1 Apr 2020
function distance = get_distance(city_1 , city_2)
p = 0; q = 0;
name_1 = strcmpi(city_1,raw(:,1));
p = find(name_1 == 1);
name_2 = strcmpi(city_2,raw(1,:));
q = find(name_2 ==1);
if p > 1 && q > 1
distance = raw{p, q};
else
distance = -1;
end
##### 6 CommentiMostra 4 commenti meno recentiNascondi 4 commenti meno recenti
Rohit Singh Chauhan il 21 Set 2020
function distance = get_distance(city1,city2)
[row col] = size(everything);
c1 = 0;
c2 = 0;
for ii = [1:row]
c1_row = everything{ii,1};
if strcmp(c1_row,city1)
c1 = ii
end
end
for jj = [1:col]
c2_col = everything{1,jj};
if strcmp(c2_col,city2)
c2 = jj
end
end
if (c1==0 || c2==0)
distance = -1
else
distance = everything{c1,c2}
end
end
Lucero il 24 Giu 2022
thanks

Accedi per commentare.

This is how i done it using for loop, make it simple to understand.
function distance = get_distance(a,b)
x = size(raw)
v = a
c = b
for i=2:x(1,1)
y = raw{1,i};
if strcmp(y,v)
g=1
for j= 1:x(1,2)
z = raw{j,1};
if strcmp(z,c)
f=1
distance = raw{i,j};
break
else
distance = -1;
end
end
break
else
distance = -1;
end
end
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
manish Singh il 20 Giu 2021
Brother can you elaborate your code

Accedi per commentare.

Mulayam Singh Choudhary il 18 Giu 2020
Modificato: Mulayam Singh Choudhary il 18 Giu 2020
function distance=get_distance(a,b)
a= convertCharsToStrings(a);
b= convertCharsToStrings(b);
ii=2;
for k=1:length(raj)-1
raj{1,ii}= convertCharsToStrings(raj{1,ii});
raj{ii,1}= convertCharsToStrings(raj{ii,1});
ii=ii+1;
end
ii=2;
jj=2;
for m=2:length(raj)
if raj{1,ii}==a
break;
end
ii=ii+1;
end
for n=2:length(raj)
if raj{jj,1}==b
break;
end
jj=jj+1;
end
if ii>length(raj)||jj>length(raj)
distance=-1;
else
distance=bar(jj-1,ii-1);
end
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Prince Raj Yadav il 26 Lug 2020
Get_Distance
function distance = get_distance(a,b)
[row,col] = size(raw);
for i = 2:row
if strcmp(raw{i,1},a) == 1
break;
else
i = 1;
end
end
for j = 2:col
if strcmp(raw{1,j},b) == 1
break;
else
j = 1;
end
end
if i > 1 && j > 1
distance = raw{i,j};
else
distance = -1;
end
end
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Rik il 26 Lug 2020
Same here. You don't add any comments, nor do you provide any reasons for the functions you're using. Why did you bother post this? (these are not meant as rhetorical questions)

Accedi per commentare.

### Categorie

Scopri di più su Programming 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