working in matlab, i have a 5x4 matrix. each number in the matrix represents a c
ID: 3717892 • Letter: W
Question
working in matlab, i have a 5x4 matrix. each number in the matrix represents a city. im trying to make an array that turns the city number into the city name so i can display it. my code looks like this
for p=1:stops
for c=1:stops-1
if citytracker(p:c)==1
citydisplay(p,c)='Amherst';
elseif citytracker(p:c)==2
citydisplay(p,c)='Springfield';
elseif citytracker(p:c)==3
citydisplay(p,c)='Boston';
elseif citytracker(p:c)==4
citydisplay(p,c)='Greenfield';
elseif citytracker(p:c)==5
citydisplay(p,c)='Worcester';
end
end
end
it doesnt work, i get citys in a diagonal. any ideas?
Explanation / Answer
Sol: I suppose you are getting the cities in the diagonal of the array 'citydisplay' and the numbers are stored in the array 'citytracker'. One problem I can see here is with this code:
if citytracker(p:c)==1
citydisplay(p,c)='Amherst';
instead of this, use this-
if citytracker(p,c)==1
citydisplay(p,c)='Amherst';
and the same for other elseif statements. Array elements in MATLAB are accessed with (row,column) and not (row:column), so this should work.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.