MATLAB
MATLAB
In MATLAB, a variable is like a storage container for data. You dont need to define the typeMATLAB
figures it out!
x = 10; % A number
Rules:
Tip: Use meaningful names like `averageScore` instead of just `a` or `x`.
2. Vectors and Matrices
Vectors:
- Row vector: a = [1 2 3]
Matrix:
- A = [1 2 3; 4 5 6]
Differences:
Use Case:
1. For Loop:
Example:
for i = 1:5
disp(i)
end
2. While Loop:
Example:
i = 1;
while i <= 5
disp(i)
i = i + 1;
end
Tip: Make sure your while loop ends; otherwise, it runs forever!
4. Condition in MATLAB
Example:
x = 5;
if x > 0
disp('Positive')
elseif x == 0
disp('Zero')
else
disp('Negative')
end
Types:
- if
- if-else
- if-elseif-else
- Nested conditions
- && (and)
- || (or)
- ~ (not)