Hello everyone. I’ve been enjoying my spring break from school. I’ve looked over material for a school project and plan on applying for jobs this weekend. I hope your week is going well. Today I wanted to describe two types of functions in Matlab, the loop and conditional, as it is used in coding for Matlab. A loop is a function used to repeat a cycle. A conditional is used to decide which calculation or function to perform based on a set of criteria. Matlab has four types of loops and conditionals available by default: if conditional, while loop, for loop, and switch conditional. In this post, I will describe to you each type, how to use them, and give a basic example.
Description: The if conditional is a function that takes a piece of data, checks for a mathematical condition, and then uses that to apply the desired function.
Standard Usage: This function is commonly used to perform calculations that accurately account for large quantities of diverse data.
Example: In this case, y=-2+2*4=6
if x < 0
elseif x < 2
Description: The while loop executes a function as long as a mathematical condition is true.
Standard Usage: This is commonly used to iterate a process until a specific criterion is met.
Example: In this case, y=1+2+3+4+5+6+7+8+9=45
while x < 10
Description: This is a loop that executes a function for a set number times.
Standard Usage: This is commonly used to perform calculations on a specific set of data for each individual data element. This can also be used to perform calculations for cases where a specific number of iterations are used.
for i = 1:length(x)
Description: This is a conditional that takes a value or string variable and uses it choose which calculation to perform.
Standard Usage: This is commonly used to write a program to execute a variety of calculations depending on a user specified situation.
Example: In this case, y=4
disp(‘y is unknown’)
These are the four types of loops expressed in a very basic way. This information can also be found by typing help and the name of the function, ie ‘help if’. However, I thought this would be helpful because this shows them in their simplest form while also telling you when and how they are used. When learning to program in Matlab, the issues I found when using loops and conditionals were not always how to find the information but figuring what function to use and how to use it in a specific circumstance. Hopefully, this post will help with that if you are initially learning this language and get stuck with these types of functions.
If you a learning how to use loops in Matlab right now, what do you find difficult and easy? If you already have experience using this, what did you find difficult and easy when you first learned this? Is there still anything specific about these loops and conditionals that cause issues? Let me know if you have any questions and have a good week!