# can anyone help with matlab program

#### dtrent258

Joined Jul 12, 2014
5
I very new to matlab, and have a problem i'm stuck on. I have a 8 x 15 matrix:

a=
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0=walls, obstacles
0 2 3 3 3 3 3 3 3 3 3 3 3 3 0
0 1 3 3 3 3 3 3 3 3 3 3 3 3 0
0 3 3 3 3 3 3 3 3 3 3 3 3 3 0
0 3 3 3 3 3 3 3 3 3 3 3 3 3 0
0 3 3 3 3 3 3 3 3 3 3 3 3 3 0
0 3 3 3 3 3 3 3 3 3 3 3 3 3 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

I have to write a program that simulates a robot that starts on a(3,2) and covers 100%, excluding the 0s, of the matrix by going down the second column,while changing the 3s to 4s, then moving to the third and going up, and so on. the robot only moves one space at a time and must return to starting position when finished with the matrix. I have some script for the first column, but only works for the first move. can anyone please help, I would be greatful for even push in the right direction.

#### shteii01

Joined Feb 19, 2010
4,644
I think I would do nested for loops for each column.

Rich (BB code):
for( column=2 ){
for( row=3; row<=7; row++ ){
if( a(row,column) == 3 ){
change content of location a(row,column) to 4}}}
This should cycle you through column 2 until you hit the row 7. You never enter row 8.

Then you do the above for the next column. Only you start at row 7 and move up to row 2.

And so on.

#### djsfantasi

Joined Apr 11, 2010
7,911
Is (3,2) row 3 column 2 or column 3 row 2?

Is this matrix the only one you have to process? I.e., is it safe to assume that rows 1 and 8 as well as columns 1 and 15 will only contain zeroes? Also, will there be other 1s and 2s interspersed?

I don't see how the previous example processes the rows in alternating directions. For that reason, I wouldn't use for loops.

I'd start at the specified position, and make the change to a 4 if it contained a 3.
Then I'd calculate the next position based on the following general algorithm.
Adjust row by the current direction (I.e., increment or decrement)
If the next cell is a zero, increment the column and change the direction of row traversal (increment becomes decrement and vice versa. This can be done with a variable for direction, with a value of 1 for increment or -1 for decrement).
Once the next cell is determined, perform the test for 3 again.
If the next cell is also a 0, you've reached the end of the matrix.

#### shteii01

Joined Feb 19, 2010
4,644
Is (3,2) row 3 column 2 or column 3 row 2?

Is this matrix the only one you have to process? I.e., is it safe to assume that rows 1 and 8 as well as columns 1 and 15 will only contain zeroes? Also, will there be other 1s and 2s interspersed?

I don't see how the previous example processes the rows in alternating directions. For that reason, I wouldn't use for loops.
The next column would be like this:
Rich (BB code):
for( column=3 )
{
for( row=7; row=>2; row--)
{
if( a(row,column)==3 )
{
a(row, column)=4;
}}}
You stay in column 3, but you start at row 7, check cell a(7,3), if it has 3, you replace it with 4. Then you subtract 1 from the row, since row is 7, when you subtract 1, you move to row 6, meaning you moved up. Check the cell. If cell holds 3, replace 3 with 4. And so on until they finish row 2. Then they switch to column 4 and go down, like they did in column 2.

#### dtrent258

Joined Jul 12, 2014
5
Thank you for the help so far. Shteii01, I tried the code you suggested and could not get it to work for me, I'm sure the fault is on my side but after some work I came up with this:
p=size(a);
n=p(1);
m=p(2);
for i=1:1:n;
for j=1:1:m;
if a(i,j)>2;
a(i,j)=4
end
end
end
a

It works somewhat but goes left to right, left to right, instead of up, down, up, and also does not alternate.
In response to you djsfantasi, by (3,2) I mean rows, columns, and no this is not the only matrix it needs to work on, it should work on any matrix of the same size, this matrix is just to practice on. the real matrix will be 30 x 25 and contain obstacles that need to be avoided as well. somthing like this:

0 0 0 0 0 0 0 0 0 0 0 0 0
0 2 3 3 3 3 3 3 0 0 3 0 0
0 1 3 3 3 3 3 3 0 0 3 0 0
0 3 3 3 3 3 3 3 0 0 3 0 0
0 3 3 3 3 3 3 3 3 3 3 3 0
0 3 3 3 0 0 0 3 3 3 3 3 0
0 3 3 0 0 0 0 3 3 3 3 3 0
0 3 3 3 3 3 3 3 3 3 3 3 0
0 0 0 0 0 0 0 0 0 0 0 0 0

2=charging station
1=robot
3=floor
0=obstacle

#### shteii01

Joined Feb 19, 2010
4,644
I think your code should look like this:

The doc file has the output and some of my notes.

#### Attachments

• 91 KB Views: 40
• 33.5 KB Views: 14
Last edited:

#### djsfantasi

Joined Apr 11, 2010
7,911
And here is another approach, using some of the algorithm I described. I didn't check for the zeroes, as your second example included embedded obstructions. It's my understanding that you're not traversing the matrix as a maze, but rather applying some simple processing rule to the cell contents.

Rich (BB code):
// set up for the first pass
MyDirection=1;
myBegin=3;
myEnd=7;
myColumn=2;

//process each column
While (myColumn<15){
// for each row...
For (myRow=myBegin:
myRow<=myEnd:
myRow+=myDirection){
If myCell(myRow,myColumn)==3 myCell(myRow,myColumn)=4;
}
}
// next column
myColumn+=1;
// reverse direction of row traversal
If (myDirection=-1){
myDirection=1;
myEnd=7;
myBegin=2;
}  else {
myDirection=-1;
myEnd=2;
myBegin=7;
}
}
// set indices to beginning
myRow=3;
myColumn=2;

Last edited: