Lab for traversing 2D Matricies using nested loops
Initial Publication Date: October 15, 2024
Summary
The activity starts with the creation of a large 2d matrix with random numbers (or letters) that represent the proteins needed to build hydro-codons and then asks the students to traverse the matrix in all 8 possible directions (left to right, right to left, top-down, bottom-up, both directions on the main/principle diagonal, and both directions on the anti/counter diagonal) to find if any of the adjacent proteins form one of the known hydro-codons that exist inside of an RNA strand. Then they expand to figuring out if their matrix includes an RNA strand. Lastly the students get to use switch cases to make decisions.
Learning Goals
- The students will learn how to use nested for loops to traverse through a 2D matrix in all 8 possible directions: left to right, right to left, top-down, bottom-up, both directions on the main/principle diagonal, and both directions on the anti/counter diagonal.
- The students will learn to use Switch-cases in Matlab to make decisions.
- The students will learn to use Switch-cases in Matlab to make decisions.
Context for Use
- This activity is for beginners to programming and/or Matlab.
- This activity is designed to be a lab activity but can easily be used as an assignment or project.
- The students will learn how to create nested for loops and traverse a 2D matrix in all 8 possible directions: left to right, right to left, top-down, bottom-up, both directions on the main/principle diagonal, and both directions on the anti/counter diagonal. The students also get to learn how to use Switch-cases in Matlab.
- Even though the activity includes traversing through a matrix that represents data related to hydro-codons and RNA strands, no bio knowledge is needed as anything required is covered in the assignment description.
- The activity is hard due to its size and the fact that the students are beginners. However, it is an easy assignment for students who have become comfortable with nested for loops and Matlab.
- The students will need to know how to create random valued matrices and use "for" loops
- This activity is designed to be a lab activity but can easily be used as an assignment or project.
- The students will learn how to create nested for loops and traverse a 2D matrix in all 8 possible directions: left to right, right to left, top-down, bottom-up, both directions on the main/principle diagonal, and both directions on the anti/counter diagonal. The students also get to learn how to use Switch-cases in Matlab.
- Even though the activity includes traversing through a matrix that represents data related to hydro-codons and RNA strands, no bio knowledge is needed as anything required is covered in the assignment description.
- The activity is hard due to its size and the fact that the students are beginners. However, it is an easy assignment for students who have become comfortable with nested for loops and Matlab.
- The students will need to know how to create random valued matrices and use "for" loops
Description and Teaching Materials
Please see the attached assignment document.
Lab Assignment for Loops (Acrobat (PDF) 813kB Oct15 24)
Lab Assignment for Loops (Acrobat (PDF) 813kB Oct15 24)
Teaching Notes and Tips
- The large size of the initial 2D array may pose a problem for student systems so they may want to build everything out using a much smaller 2D matrix before they test it on a larger one. They also may choose to just use a more powerful lab machine.
- If students realize that the code for all 8 directions are very similar and simply the modification of the bounds, direction, and order of the for loop index counter initialization and termination conditions, then they will not struggle with the assignment.
- If students realize that the code for all 8 directions are very similar and simply the modification of the bounds, direction, and order of the for loop index counter initialization and termination conditions, then they will not struggle with the assignment.
Share your modifications and improvements to this activity through the Community Contribution Tool »
Assessment
- The assignment requires certain outputs which if checked for accuracy will determine the outcome.
- Post this assignment, students should have no problem traversing a 2D array using nested for loops in any direction.
- Post this assignment, students should have no problem traversing a 2D array using nested for loops in any direction.