While Loop problems

I am trying to write a while loop. I want to read a matrix first, once ive done this i want to ask the user for co-ordinates, if those co-ordinates lie outside the matrix i want to display "these co-ordinates dont lie within the mining area" since the matrix displays a mine.
Once a suitable set of co-ordinates within the matrix is established i then want to ensure that the value in the matrix selected is zero, should the value not be zero i want to ask the user for a new set of co-ordinates.
Here's what i have so far,i feel like i lmost have it worked out however i just cant get the last part to work.
dirt_grid = csvread('dirtgrid.csv');
for base_station_x = input('Enter an x co-ordinate for the base station: ' );
if base_station_x > length (dirt_grid)
disp('The x co-ordinate is outside the mining area, please enter another co-ordinate')
base_station_x = input('Enter an x co-ordinate for the base station: ' );
end
end
for base_station_y = input('Enter an y co-ordinate for the base station: ' );
while base_station_y > length (dirt_grid)
disp('The y co-ordinate outside mining area, please enter another co-ordinate')
base_station_y = input('Enter an y co-ordinate for the base station: ' );
end
end
base_station = dirt_grid (base_station_x, base_station_y);
while base_station >= 1
disp('The location you have entered is not a valid location')
end
I am aware that this program forms a infinite loop but im not sure where i should place this last loop to make the problem work. Thanks for any assistance anyone is able to provide...

 Risposta accettata

Teja Muppirala
Teja Muppirala il 4 Mag 2011

0 voti

This is one way to go about doing it. (x is from left to right, and y is from top to bottom.)
dirt_grid =[ 0 1 1 0 1
1 1 0 0 1
1 1 1 0 0];
base_location_is_zero = false;
while ~base_location_is_zero
try
isok = 0;
while ~isok
base_station_x = input('Enter an x co-ordinate for the base station: ' );
if ~ismember(base_station_x, 1:size(dirt_grid,2))
disp('The x co-ordinate is outside the mining area, please enter another co-ordinate')
else
isok = 1;
end
end;
isok = 0;
while ~isok
base_station_y = input('Enter an y co-ordinate for the base station: ' );
if ~ismember(base_station_y, 1:size(dirt_grid,1))
disp('The y co-ordinate is outside the mining area, please enter another co-ordinate')
else
isok = 1;
end
end;
base_station = dirt_grid (base_station_y, base_station_x);
if base_station ~= 0
disp('The location you have entered is not a valid location')
else
base_location_is_zero = true;
end
catch
disp('Bad input. Put in an integer and try again.')
end
end;
disp('Everything is ok!');

Più risposte (0)

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by