Please help with script in matlab (avoiding objects in a matrix)

In summary: Is your algorithm:1. Move down, and if you can't move down then move left, if you can't move left then move up, if you can't move up then move right, and finally if you can't move right then you should stop cleaning.If your algorithm is like that then you'd be better off with a single for loop and a switch statement, right?The alternative is to think of the problem as a matrix and use the coordinates of the elements to tell the computer how to move around the matrix. If that is what you want to do then you need to think of what each iteration of the loop should do and how it should change the value of i and j (which represent the coordinates of the element in
  • #1
dtrent258
4
0
1. The problem statement.
Create a function in MATLAB for a simulation of a automated robot vacuum to avoid obstacles while cleaning an undisclosed floor plan. the floor plan which will be in the form of a matrix not known beforehand. The robot (represented by a value of 1) can move horizontally (left to right, down one, right to left...) or vertically (top to bottom, over one, bottom to top...), but the robot should "clean" 100% of the floor plan even if the surface has a value of a clean tile (represented by a 4). While traversing the matrix the robot will encounter dirty floor (represented by the value 3) as well as obstacles (represented by the value 0). when a dirty tile is encountered value will be changed to a "4". When an obstacle is encountered the robot should go around it instead of just skipping over, and then continue on cleaning. once the matrix is cleaned the robot will return to the charger (represented by a 2). I've some code written but can not seem to find a good way of avoiding obstacles and return to the same column or row I was on. If anyone has any suggestions it would be greatly appreciated. I've included a sample matrix just for me to practice on, the real matrix will be 30 rows by 25 columns. Values in the matrix are as follows:

1= robot
2= charger (always located at (2,2) in the matrix, also robot will always begin here as well)
3= dirty floor
4= clean floor
0= wall or obstacle (the perimeter of the matrix will always be a wall, but the interior will vary from matrix to matrix)




Homework Equations


My code has the robot moving up and down. I have tried writing script to move the robot left or right depending on which way isn't blocked. but I have to do that for each time it hits an obstacle, plus I can't find a good way to return to the row or column that I was on.


The Attempt at a Solution



a=xlsread('robmat');
p=size(a);
n=p(1);
m=p(2);
time=0; % time taken to clean(1 for clean, 5 for dirty)
bat=0; % battery life used ( +1 for every move)
for i=3:1:n-1;
for j=2;​
if a(i,j)==3;​
a(i,j)=4;​
time=time+5;​
bat=bat+1;​
if a(i,j)==4;​
time=time+1;​
bat=bat+1;​
end​
end​
end​
end
for i=n-1:-1:2;
for j=3;​
if a(i,j)==3;​
a(i,j)=4;​
time=time+5;​
bat=bat+1;​
if a(i,j)==4;​
time=time+1;​
bat=bat+1;​
end​
end​
end​
end
a


[fprintf('Time taken equals: %d seconds\n',time);]
[fprintf('Amount of battery charge used equals: %d\n',bat);]
 

Attachments

  • robmat.xlsx
    8 KB · Views: 208
Last edited:
Physics news on Phys.org
  • #2
Welcome to PF!

One way to help people respond to your question is to edit your post and add [ code ] tags (no spaces between the square brackets and the code tag kind of like html formatting) around the MATLAB script so that it will be properly boxed in and will preserve any code indentation you may have.

As an example using a portion of your code:

Code:
...

for i=n-1:-1:2;
    for j=3;
        if a(i,j)==3;
            a(i,j)=4;
            time=time+5;
            bat=bat+1;
        if a(i,j)==4;
            time=time+1;
            bat=bat+1;
    end
end

...

which I assume is what you meant
 
  • #3
Alright thanks, I'll try and edit it so it's easier to read. I should probably mention that this is my first MATLAB class I've taken, so I'll most likely make a lot of mistakes, but I'm trying my best to keep up with it. thanks again.
 
  • #4
I think that I've made it easier to read, I hope that this helps some.
 
  • #5
Okay there are numerous problems with your script:

the i,j for loops should be something like this, basically the inner j for loop must have some sort of range, right?

Code:
for i=3:1:n-1;

    for j=2:1:n-1:

...

    end
end

next the nested if statements are a problem how can the inner if ever be true if the outer one is true.

I haven't studied your logic yet but wanted to point out these two things
 
  • #6
I think I see what you're saying. So I should replace the inner if statement with an elseif statement?.
 
  • #7
dtrent258 said:
I think I see what you're saying. So I should replace the inner if statement with an elseif statement?.

That depends on what you're trying to do, right?

You need to understand how you would solve in order to tell the computer how to solve it by thinking like a computer.
 

1. How can I avoid objects in a matrix using a script in MATLAB?

To avoid objects in a matrix in MATLAB, you can use the "find" function to identify and remove the objects. You can also use the "isnan" function to check for any NaN values in the matrix and replace them with zeros.

2. Can I use a loop to avoid objects in a matrix in MATLAB?

Yes, you can use a loop in your script to iterate through the matrix and check for objects using conditional statements. However, using vectorized operations is generally more efficient and recommended.

3. How do I handle large matrices when avoiding objects in MATLAB?

When working with large matrices, it is important to use efficient methods such as vectorization and preallocation of memory. You can also consider breaking down the matrix into smaller chunks to improve performance.

4. Can I avoid specific types of objects in a matrix using a script in MATLAB?

Yes, you can specify the type of objects you want to avoid in the matrix by using logical indexing and conditional statements. For example, you can use the "isnumeric" function to check for numerical values and exclude them from your matrix.

5. Is there a built-in function for avoiding objects in a matrix in MATLAB?

No, there is no specific built-in function for avoiding objects in a matrix in MATLAB. However, there are various functions and techniques that can be used together in a script to achieve this task, such as "find", "isnan", and logical indexing.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
826
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
825
  • Engineering and Comp Sci Homework Help
Replies
3
Views
809
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
Back
Top