This document provides an introduction to basic MATLAB concepts including:
1) Clearing variables, closing figures, and clearing the command window. It also discusses commenting code with % and suppressing output with ;.
2) Creating vectors and matrices by assigning values between brackets [], including column and row vectors.
3) Common vector and matrix math operations like transpose, element-wise multiplication, squaring elements, creating vectors and matrices of specified dimensions with functions like zeros, ones, and rand.
4) Indexing and slicing vectors and matrices to access individual elements or entire rows/columns. Finding vector lengths and matrix dimensions.
5) Using for loops to iterate over vectors and matrices, including initializing counters and setting loop limits
This document provides an introduction to basic MATLAB concepts including:
1) Clearing variables, closing figures, and clearing the command window. It also discusses commenting code with % and suppressing output with ;.
2) Creating vectors and matrices by assigning values between brackets [], including column and row vectors.
3) Common vector and matrix math operations like transpose, element-wise multiplication, squaring elements, creating vectors and matrices of specified dimensions with functions like zeros, ones, and rand.
4) Indexing and slicing vectors and matrices to access individual elements or entire rows/columns. Finding vector lengths and matrix dimensions.
5) Using for loops to iterate over vectors and matrices, including initializing counters and setting loop limits
This document provides an introduction to basic MATLAB concepts including:
1) Clearing variables, closing figures, and clearing the command window. It also discusses commenting code with % and suppressing output with ;.
2) Creating vectors and matrices by assigning values between brackets [], including column and row vectors.
3) Common vector and matrix math operations like transpose, element-wise multiplication, squaring elements, creating vectors and matrices of specified dimensions with functions like zeros, ones, and rand.
4) Indexing and slicing vectors and matrices to access individual elements or entire rows/columns. Finding vector lengths and matrix dimensions.
5) Using for loops to iterate over vectors and matrices, including initializing counters and setting loop limits
This document provides an introduction to basic MATLAB concepts including:
1) Clearing variables, closing figures, and clearing the command window. It also discusses commenting code with % and suppressing output with ;.
2) Creating vectors and matrices by assigning values between brackets [], including column and row vectors.
3) Common vector and matrix math operations like transpose, element-wise multiplication, squaring elements, creating vectors and matrices of specified dimensions with functions like zeros, ones, and rand.
4) Indexing and slicing vectors and matrices to access individual elements or entire rows/columns. Finding vector lengths and matrix dimensions.
5) Using for loops to iterate over vectors and matrices, including initializing counters and setting loop limits
Close all; %closes all figures* Clc; %clear command window% ; suppresses output % comments Use %--------------------------------------------% to break up code visually \theta_0 % θ0 To report outputs disp(variable); fprintf(‘some comment %g \variable’, variable) %g is general number place holder \variable puts it on a new line pi is 3.1415… Making Vectors and Matrices Name a vector b b=[1,2,3,4] %gives a column vector (horizontal)% also b=[1 2 3 4] %gives the same column vector% b=[1;2;3;4] %gives a row vector (vertical)% Open and close brackets and then fill in [] to make sure u don’t mess up brackets Name a matrix B B=[1 2 3 4; 1 2 3 4; 1 2 3 4; 1 2 3 4] or B=[ 1 2 3 4 1 2 3 4 1 2 2 4 1 2 3 4 ];
Vector and matrix math
Multiplication: a*b; b’ b transpose element by element multiplication: d=a.*b; (gives [a1*b1, a2*b2, a3*b3] in a vector or matrix, however the original a and b were formatted) to square every element in a vector or matrix: a.^2 to multiply or divide every element in a vector or matrix: a.*4 or a./5 make a vector from 0 to 10 with step size .5: x=0:0.5:10 make a 10x10 matrix of zeroes: y= zeros(10) make a 10x5 matrix of zeros: y=(10,5) make a 5x5 identity matrix: I=eye(5) make a 10x5 matrix of ones: o=ones(10,5) make a 10x5 matrix of random numbers from 0 to 1: r=rand(10x5)
Indexing and sizing
Figure out the size a matrix is: A_size=size(A) %gives a 1x2 matrix with the dimensions of A% Figure out the rows in matrix A: A_rows=size(A,1) Figure out the columns in matrix A: A_rows=size(A,2) Figure out the length of a vector from a loop or anything else: x_l=length(x) %returns the largest dimension of x% Figure out the last entry in a vector x: x{end} Find out number at position 2, 3 in A: A_23=A(2,3) Find out row 2 of A: A_row2=A(2,:) Find out column 2 of A: A_column2=A(:,2) : Everything For Loops For counter = start : step : finish do stuff; end disp(dependent variable) ex. v=input(enter a vector); i=1; %initialize the counter (index) finish=length(v); %sets finish equal to the length of the vector so you don’t have to know it for i=1:step:finish %could also just have “length(v)” in place of “finish” to make the code cleaner% display((v(i))^2) %our code will show the square root of each entry% end Plotting hold on; %makes sure everything is plotted on the same graph% box on; %makes a frame around the figure% plot(x, y(1,:), ‘rs’,2) %plot of x vs y at all values of y at index (counter) 1 with red squares with width 2% xlabel(‘NAME’); ylabel(‘NAME’); title(‘NAME’); legend(‘index 1’,’index2’,’index 3’,’index 4’) lengend(‘box off’) %to get rid of box around the legend% hold off; grid(‘on’); %if you want gridlines%