What is the concept of xor in MATLAB?

In summary, the task is to simulate a puzzle game in MATLAB where n students go through a school of n lockers, opening and closing them as they walk by. The goal is to determine which lockers are left open after all students have gone through. The suggested solution involves using the xor function to toggle the status of the lockers and considering the first student's actions as irrelevant to the final outcome.
  • #1
GE2014
11
0

Homework Statement



(see attached files)

basically, I have to solve a puzzle game simulated by matlab. In a school of n students with n lockers, each student opens and closes a locker they walk by. all the lockers start the game closed. the first student changes every locker's status (opens them). the second student changes the status of every 2ed locker. the third student changes every third locker etc etc. the goal is to determine the lockers that are left open after n students have gone through

Homework Equations



The assignment says I will use 2 for loops. one to step through all the students and one inside of the first to step through each nth locker. the 2ed for loop is pretty confusing to me.

The Attempt at a Solution



Heres what I have so far.
students=input('Enter number of students: ')
locker=zeros(1,students);
index=1;
changes=0;
for num= 1:1:students
locker(index)=changes+index;
index=index+1;
end

the code here steps through as if each student changes the status of each locker, starting with the next locker from the previous student.

the array looks like this
students=8
1 2 3 4 5 6 7 8
odd=open
even=closed


when it should look like this
1 2 2 3 2 4 2 4
 

Attachments

  • cap.JPG
    cap.JPG
    48.8 KB · Views: 901
Last edited:
Physics news on Phys.org
  • #2
Hi

Lets consider first a small example with 3 students. Initially all the lockers are closed:

locker 1 2 3
status 0 0 0

After the first student goes all the lockers are opened cause all are multiple of 1:

locker 1 2 3
status 1 1 1

Then comes the second student which closes the locker 2:

1 2 3
1 2 1

Finally comes the third one:

1 2 3
1 2 2

So you need one loop that can start for example for the student 1, and then a second loop that goes through the lockers and checks if the number of it is multiple of the number of the student, adding one if it is.

Regarding your code let's see what it does. Initially all lockers are closed:

1 2 3
0 0 0

Then the program enters the for loop. In the first iteration it changes only the locker 1 when it should have changed all(second loop missing):

1 2 3
1 0 0

At the second iteration you have the student number 2. What your program does is adding the value of index that is 2. However it should increase by one( instead of locker(index)=changes+index; you should do locker(index)=locker(index)+1) in order to have odd=closed and even=open. After this iteration you have:

1 2 3
1 2 0

Following the same logic after the third and last iteration you have:

1 2 3
1 2 3

So your program is simple copying the index of the locker to the status vector.
With this ideas in mind you should be able to do a working program :)
 
  • #3
GE2014 said:

Homework Statement



(see attached files)

basically, I have to solve a puzzle game simulated by matlab. In a school of n students with n lockers, each student opens and closes a locker they walk by. all the lockers start the game closed. the first student changes every locker's status (opens them). the second student changes the status of every 2ed locker. the third student changes every third locker etc etc. the goal is to determine the lockers that are left open after n students have gone through

Homework Equations



The assignment says I will use 2 for loops. one to step through all the students and one inside of the first to step through each nth locker. the 2ed for loop is pretty confusing to me.

The Attempt at a Solution



Heres what I have so far.
students=input('Enter number of students: ')
locker=zeros(1,students);
index=1;
changes=0;
for num= 1:1:students
locker(index)=changes+index;
index=index+1;
end

the code here steps through as if each student changes the status of each locker, starting with the next locker from the previous student.

the array looks like this
students=8
1 2 3 4 5 6 7 8
odd=open
even=closed


when it should look like this
1 2 2 3 2 4 2 4
I don't see any attached files.
 
  • #4
sorry bout that. Just uploaded it
 
  • #5
You should not use a second for loop, because that is just slow and icky for MATLAB. Instead, consider the xor of a bit with 1. If your bit is 0, it changes it to 1. If your bit is 1, it changes it to 0. Hence, we have a way to open and close your lockers.

Next, consider this sample code:
Code:
>> a = zeros(1,10)

a =

     0     0     0     0     0     0     0     0     0     0>> a(4:4:end) = xor(a(4:4:end),1)

a =

     0     0     0     1     0     0     0     1     0     0

Do you see where I am going with this?

Edit: It is worth noting, the first student adheres to the same algorithm as the other students, assuming all lockers begin closed. That is, starting at the first locker, he goes to every "1st" locker on the row. This means inside your for loop of students, you needn't put special code for his activities. E.g. consider the output of
a(1:1:end) = xor(a(1:1:end),1);

Another approach might be to initialize the lockers to open (e.g. a = ones(1,studentCount)) (since you know the first student will open them all) and just ignore him, beginning your for loop at 2:studentCount. I would do this approach since it is computationally more efficient.

You might wonder what would happen if studnetCount = 1 (What does MATLAB do with for k = 2:1, code, end ??) Just try it out. You will be pleased to find the for loop is simply skipped.
 
Last edited:
  • #6
cathode-ray said:
Hi

Lets consider first a small example with 3 students. Initially all the lockers are closed:

locker 1 2 3
status 0 0 0

After the first student goes all the lockers are opened cause all are multiple of 1:

locker 1 2 3
status 1 1 1

Then comes the second student which closes the locker 2:

1 2 3
1 2 1

Finally comes the third one:

1 2 3
1 2 2

So you need one loop that can start for example for the student 1, and then a second loop that goes through the lockers and checks if the number of it is multiple of the number of the student, adding one if it is.

Regarding your code let's see what it does. Initially all lockers are closed:

1 2 3
0 0 0

Then the program enters the for loop. In the first iteration it changes only the locker 1 when it should have changed all(second loop missing):

1 2 3
1 0 0

At the second iteration you have the student number 2. What your program does is adding the value of index that is 2. However it should increase by one( instead of locker(index)=changes+index; you should do locker(index)=locker(index)+1) in order to have odd=closed and even=open. After this iteration you have:

1 2 3
1 2 0

Following the same logic after the third and last iteration you have:

1 2 3
1 2 3

So your program is simple copying the index of the locker to the status vector.
With this ideas in mind you should be able to do a working program :)

Yea that makes sense.
I'm still pretty easily confused when it comes to for loops but here's what I think should be closer.
students=input('Enter number of students: ')
locker=zeros(1,students);
index=1;
for i=1:1:students
for j=1:1:students​
if mod (index,students(j))==0
locker(index)=Locker(index)+1
end​
end​
end


Gives me this error message:
? Attempted to access students(2); index out of bounds because
numel(students)=1.

Error in ==> lockers at 7
if mod (index,students(j))==0
 
  • #7
GE2014 said:
Yea that makes sense.
I'm still pretty easily confused when it comes to for loops but here's what I think should be closer.
students=input('Enter number of students: ')
locker=zeros(1,students);
index=1;
for i=1:1:students
for j=1:1:students​
if mod (index,students(j))==0
locker(index)=Locker(index)+1
end​
end​
end


Gives me this error message:
? Attempted to access students(2); index out of bounds because
numel(students)=1.

Error in ==> lockers at 7
if mod (index,students(j))==0

You are accessing the 1 by 1 matrix "students" at an index of 2 with students(j), which is out of bounds. numel() gives you the number of elements in a MATLAB array. A 1x1 matrix with 1 element is simply a regular, good-old fashioned constant. It's sort of like I say C = 3; B = c(2). That makes no sense!

You should consider my code since it is a much better approach.
 
  • #8
@RoshanBBQ
Probably wouldn't be good if I used your method. The point of the exercise is to practice for loops and arrays.
 
  • #9
GE2014 said:
@RoshanBBQ
Probably wouldn't be good if I used your method. The point of the exercise is to practice for loops and arrays.

You will still use a for loop to cycle through each student. There is still an array. It is not a good idea to use for loops in MATLAB when you can avoid it. Your professor will be impressed with your solution.
 
  • #10
RoshanBBQ said:
You will still use a for loop to cycle through each student. There is still an array. It is not a good idea to use for loops in MATLAB when you can avoid it. Your professor will be impressed with your solution.

Alright I'll give it a try. Could you possibly explain a little more what xor does exactly? I've never heard of it
 
  • #11
GE2014 said:
Alright I'll give it a try. Could you possibly explain a little more what xor does exactly? I've never heard of it

Yes. Let's say you have a function that takes any number (>=2) of 1s and 0s

f(b_0,b_1,b_2,...)

Certain examples would be

f(0,0,1,0)

or f(0,1)

The function is an xor if it gives a 1 if there is an odd number of 1s in the input and a 0 if there is an even number of 1s in the input.

So

f(0,0) = zero ones which is even => output 0
f(0,1) or f(1,0) = one one which is odd => output 1
f(1,1) = two ones which is even => output 0

If you just look at the output/input characteristics, you should be able to convince yourself through enumeration that if you have f(b,1), it will simply toggle b. If b is 1, it goes to 0. If b is 0, it goes to 1.

Now, in MATLAB, things work very similarly but are slightly different. xor() can take an array of doubles which is not necessarily either a 0 or a 1. It could be, for example, 1.5 or 3. So MATLAB has the same rules of operation except it outputs a 1 if there is an odd number of nonzero elements else a 0.

xor(array,1) does the xor for each element in the array. So if a = [1 1 0 0 1 1]
xor(a,1) = [0 0 1 1 0 0]

If you need to Google for it, its full name is the logical exclusive or.
 

Related to What is the concept of xor in MATLAB?

1. What is Matlab Puzzle Assignment?

Matlab Puzzle Assignment is a task or project that requires the use of Matlab software to solve a given puzzle or problem. It involves writing code and using various tools and functions in Matlab to find a solution.

2. How do I approach a Matlab Puzzle Assignment?

The first step in approaching a Matlab Puzzle Assignment is to carefully read and understand the instructions or problem statement. Then, break down the problem into smaller, manageable parts and plan out your approach. It is also helpful to research and familiarize yourself with the relevant functions and tools in Matlab before starting the assignment.

3. How important is coding experience for completing a Matlab Puzzle Assignment?

Coding experience is important for completing a Matlab Puzzle Assignment, but it is not necessary to be an expert. As long as you have a basic understanding of coding principles and syntax, and are willing to learn and explore new functions and tools, you can successfully complete the assignment.

4. Can I work with others on a Matlab Puzzle Assignment?

It is generally encouraged to work on a Matlab Puzzle Assignment on your own, as it allows you to fully understand and learn from the problem. However, you can collaborate with others as long as each person contributes equally and each person understands and can explain the code used.

5. How can I check if my Matlab Puzzle Assignment is correct?

You can check the correctness of your Matlab Puzzle Assignment by running your code and comparing the output to the expected result. You can also ask a friend or classmate to review your code and provide feedback. It is also helpful to test your code on different inputs to ensure it works for various scenarios.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
16
Views
12K
  • General Math
Replies
1
Views
3K
  • Introductory Physics Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
965
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
32
Views
3K
  • Precalculus Mathematics Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
Replies
3
Views
2K
Back
Top