# can anyone help with matlab program

Discussion in 'Programmer's Corner' started by dtrent258, Jul 12, 2014.

1. ### dtrent258 Thread Starter New Member

Jul 12, 2014
5
0
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.

2. ### shteii01 AAC Fanatic!

Feb 19, 2010
3,392
497
I think I would do nested for loops for each column.

Code ( (Unknown Language)):
1. for( column=2 ){
2.   for( row=3; row<=7; row++ ){
3.     if( a(row,column) == 3 ){
4.       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.

3. ### djsfantasi AAC Fanatic!

Apr 11, 2010
2,809
834
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.

4. ### shteii01 AAC Fanatic!

Feb 19, 2010
3,392
497
The next column would be like this:
Code ( (Unknown Language)):
1.
2. for( column=3 )
3. {
4.   for( row=7; row=>2; row--)
5.   {
6.     if( a(row,column)==3 )
7.     {
8.       a(row, column)=4;
9.     }}}
10.
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.

5. ### dtrent258 Thread Starter New Member

Jul 12, 2014
5
0
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

6. ### shteii01 AAC Fanatic!

Feb 19, 2010
3,392
497
I think your code should look like this:

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

File size:
91 KB
Views:
40
• ###### robot matrix.doc
File size:
33.5 KB
Views:
14
Last edited: Jul 13, 2014
7. ### djsfantasi AAC Fanatic!

Apr 11, 2010
2,809
834
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.

Code ( (Unknown Language)):
1. // set up for the first pass
2. MyDirection=1;
3. myBegin=3;
4. myEnd=7;
5. myColumn=2;
6.
7. //process each column
8. While (myColumn<15){
9.   // for each row...
10.   For (myRow=myBegin:
11.          myRow<=myEnd:
12.          myRow+=myDirection){
13.   If myCell(myRow,myColumn)==3 myCell(myRow,myColumn)=4;
14. }
15. }
16. // next column
17.   myColumn+=1;
18. // reverse direction of row traversal
19.   If (myDirection=-1){
20.       myDirection=1;
21.       myEnd=7;
22.       myBegin=2;
23.    }  else {
24.       myDirection=-1;
25.       myEnd=2;
26.       myBegin=7;
27.   }
28. }
29. // set indices to beginning
30. myRow=3;
31. myColumn=2;
32.

Last edited: Jul 13, 2014
8. ### dtrent258 Thread Starter New Member

Jul 12, 2014
5
0
Thanks so much you guys, you have been very helpful. I may have some questions in the near future, but hopefully I can finish it myself after the help you guys gave me. Thanks again.