Assignment question based on excel file I/O
Información
This question is locked. Vuélvala a abrir para editarla o responderla.
Mostrar comentarios más antiguos
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('Alexandria, VA' , 'Allentown, PA') should return 195
. If one or both of the specified cities are not in the file, the function returns -1.

The code I tried is the following:
I get error in the " if text{i,1} == a" and "if text{1,j} == b" lines stating matrix dimensions must agree. Will be really helpful if someone can help me with it.
function distance = get_distance(a,b)
[~,text,raw] = xlsread('Distances.xlsx');
for i = 2 : size(text,1)
if text{i,1} == a
for j = 1 : size(text,2)
if text{2,j} == b
distance = raw{i,j};
else
distance = -1;
end
end
else
distance = -1;
end
end
4 comentarios
Siddharth Vidyarthi
el 29 de Mzo. de 2019
have u got the correct answer for this?
if u got please post the code here
Sanket khullr
el 2 de Abr. de 2019
function distance = get_distance(a,b)
[~,text,raw] = xlsread('Distances.xlsx');
for i=2:size(raw,1)
if strcmp(text{i,1}, a)
break
end
end
if i>=size(raw,1)
distance=-1;
else
for j=2:size(raw,2)
if strcmp(text{1,j}, b)
distance=raw{i,j};
break
end
end
if j>=size(raw,2)
distance = -1;
end
end
Guillaume
el 2 de Abr. de 2019
@sanket,
Loops are not needed at all for this. strcmp is perfectly capable of the searching a whole cell array at once.
Walter Roberson
el 2 de Abr. de 2019
This code is wrong for the case where the match is the last row or column. You break out of the loop but the value of the index happens to exactly equal to the size so you think there was no match.
Respuestas (7)
function distance=get_distance(city_1,city_2)
n=0;m=0;
[value,name]=xlsread('Distances.xlsx');
for ii=1:size(name,2)
if (strcmp(city_1,name(ii,1)))
n=ii;
end
end
for jj=1:size(name,1)
if (strcmp(city_2,name(1,jj)))
m=jj;
end
end
if(n==0 && m==0 && n==1&&m==1)
distance =-1;
elseif (n>0&&m>0)
distance=value(n-1,m-1);
else
distance=-1;
end
Geoff Hayes
el 17 de Feb. de 2019
Ajai - if you try to compare strings using the == operator, then you will see this error if the two strings being compared are of different lengths. Instead, you should be using strcmp or strcmpi to check for equality between two strings. Your code would then change from
text{i,1} == a
to
strcmp(text{i,1}, a)
Also, rather than using a loop, you could use find to see if the first column of text contains the cities that you are looking for (see the examples in the provided link for details).
4 comentarios
Ajai Kannan
el 18 de Feb. de 2019
Tashu Bar
el 16 de Mzo. de 2019
I am facing some issue in writing code using find for simillar problem.
Geoff can you help to write the code,
Priyamvada Shankar
el 23 de Mzo. de 2019
Ajai Kannan if you got the correct code.. please post here
Walter Roberson
el 23 de Mzo. de 2019
This is obviously an assignment. We seldom post the code to answer assignments. However, if you post your attempt and indicate the error messages you are encountering, then we will assist you in debugging what you have.
ABINAND PANDIYAN
el 24 de Abr. de 2020
Editada: ABINAND PANDIYAN
el 25 de Abr. de 2020
% This problem is right I think.
function distance = get_distance(A,B)
[~,~,dist] = xlsread('Distances.xlsx');
for i=2:(size(dist,1)+1)
if i>size(dist,1)
distance=-1;
return
end
if strcmp(dist{i,1}, A)
break
end
end
for j=2:(size(dist,2)+1)
if j> size(dist,2)
distance=-1;
return
end
if strcmp(dist{1,j}, B)
distance=dist{i,j};
break
end
end
end
2 comentarios
Walter Roberson
el 24 de Abr. de 2020
Suppose that A happens to exactly match the city name in the last row. Then i = size(dist,1) and success would be desired. The for loop would execute and on the last iteration, strcmp(dist{i,1},A) would succeed, so you would break; with i = size(dist,1). Then you do if i>=size(dist,1) and find that i == size(dist,1) so the if succeeds, and you declare the distance to be -1 and return, even though you had a match.
In the case where there was no match at all, the for i loop would complete, and afterwards i would be size(dist,1), same as if there had been a match on the last item. You test i>=size(dist,1) and that is true so you return -1, same as the case where you had a city match on the last row.
In MATLAB, after a for loop, if the body of the for did not change the loop variable, then the loop control variable is left at the last value it had, not one more than the last value it had. You will need to fix that termination condition.
ABINAND PANDIYAN
el 25 de Abr. de 2020
Editada: ABINAND PANDIYAN
el 25 de Abr. de 2020
@Walter Roberson
Yeah I checked that, But now after hours of brainstroming I think I got the answer right with the edited content. You can check it now.
Fazlul Haque
el 15 de Mayo de 2020
1 voto
done without using loops

8 comentarios
Walter Roberson
el 15 de Mayo de 2020
You can omit the find() step: your a and b will be logical indexes that should work fine to pull out the needed data.
Suraj Gawade
el 19 de Mayo de 2020
It will not show value for non-existant cities.
Walter Roberson
el 19 de Mayo de 2020
The posted code appears to return -1 for non-existent cities.
Krashank Kulshrestha
el 4 de Jun. de 2020
so what should we do in this code I applied 0 in place of -1 its showing error in non existing cities
Krashank Kulshrestha
el 4 de Jun. de 2020
also please tell the reason behind applying -1 rather than 0
Walter Roberson
el 4 de Jun. de 2020
It is a requirement of the assignment, ". If one or both of the specified cities are not in the file, the function returns -1."
In terms of why, the answer is that it is valid for someone to ask about the distance between a city and the same city, and the distance that should be returned for that would be 0. If you returned 0 for unknown cities as well, then the 0 that was returned for city-to-itself could not be distinguished from the 0 that would be returned for the unknown-city case.
The place I live now, historically was a small village. There was a slightly larger village what is now two blocks away, half a kilometre. But historically miles were used, and to the nearest mile, the distance rounds down to 0. It is valid to ask about the distance between the two places, and the answer would be validly 0 ("to the nearest mile"), and that is a different situation than asking about the distance and being given a response that does not allow me to tell whether the two places are very close or if it does not have any information about one of the places.
Farhaan Zaidi Bhat
el 13 de Dic. de 2020
Editada: Farhaan Zaidi Bhat
el 13 de Dic. de 2020
Can you please explain the a and b steps? The data part?
Rik
el 13 de Dic. de 2020
Did you read the documentation for the strcmpi function?
Muhammad Qaisar Ali
el 27 de Jun. de 2020
function distance = get_distance(city_1,city_2)
persistent distt city_names;
if isempty(distt) && isempty(city_names) % Ceck wheater first time load or not.
[distt,city_names,~]=xlsread('Distances.xlsx'); % Reading ditances and names of cities
end
rowIndex = find(ismember(city_names(:,1),city_1)); % Searching city_1 in col_1 of excell seet.finding row index.
colIndex = find(ismember(city_names(1,:),city_2)); % Searching city_2 in row_1 of excell seet.finding col index.
if isempty(rowIndex) || isempty(colIndex) % If cities nit found return -1.
distance=-1;
else % Find and retun distance.
distance=distt(rowIndex-1,colIndex-1);
end
end
2 comentarios
Walter Roberson
el 27 de Jun. de 2020
Caching the data is often a good performance improvement, but remember to document the method that would need to be used to signal that the Distances file had changed.
Muhammad Qaisar Ali
el 28 de Jun. de 2020
Thanks for your vaulable feedbak..indeed it helps me alot..
Priyamvada Shankar
el 22 de Mzo. de 2019
0 votos
Can you please write the code...it will be a great help... please I have tried too many times but still getting some problems
7 comentarios
Siddharth Vidyarthi
el 29 de Mzo. de 2019
function distance = get_distance(a,b)
[~,text,raw] = xlsread('Distances.xlsx');
for i = 2 : size(text,1)
if strcmp(text{i,1}, a)
for j = 1 : size(text,2)
if strcmp(text{2,j}, b)
distance = raw{i,j};
else
distance = -1;
end
end
else
distance = -1;
end
end
please tell whats wrong in this code its showing error
Walter Roberson
el 29 de Mzo. de 2019
You do not stop looping when you find a match.
Siddharth Vidyarthi
el 29 de Mzo. de 2019
can u please eloborate in which line i have to stop?
should i use end command?
Walter Roberson
el 29 de Mzo. de 2019
The empty line after you assign raw to distance.
end is not a command. You want to find some way to break out of the loop early.
Siddharth Vidyarthi
el 29 de Mzo. de 2019
so how should i end the loop after that please help me i am getting error again and again
Walter Roberson
el 29 de Mzo. de 2019
You want to find some way to break out of the loop early.
sadek kouz
el 18 de Mzo. de 2020
Editada: sadek kouz
el 18 de Mzo. de 2020
use break to break out of the looo
XINYI CAI
el 16 de Mzo. de 2021
0 votos
why my answer does not work for non-exsitent city?
This question is locked.
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!