v={begin}:{step}:{end}
Here are some examples,
x=0:.25:1 % x=[0, .25, .5, .75, 1] x=1:-.25:0 % x=[1, .75, .5, .25, 0] x=1:2:10 % x=[1, 3, 5, 7, 9] B=A(:) % stacks the columns of A.
So if A is 4 by 6, then B=A(:) is a column vector with 24 rows. You can reshape B with the command C=reshape(B,n,m) where the only restrictions are that n and m are positive integers with m*n=24. For example to rebuild A, type reshape(B,4,6).
A=[1 2 3 4;5 6 7 8; 9 10 11 12] B=A(:) transA=A' C=transA(:) D=reshape(C,6,2) E=reshape(C,4,3) F=reshape(B,3,4)
As another example, suppose that A is an n by m matrix and v is a column vector of length n*m. Then A(:)=v replaces the elements of A by the elements of v (by columns).
v=(-12:-1)' A(:)=v
A=[1 2 3 4;5 6 7 8; 9 10 11 12] B=A(:,[2 4]) C=A([1 3],:)
E=[]
The empty matrix can be used in many ways, for example
a) to initialize a matrix for later use and
b) to delete elements of a matrix A(i:j,:)=[] simply deletes the
i through jth rows of A, i.e. this is the same as
A=[A(1:(i-1),:);A((j+1):m,:)]
A(:,[2 4])=[]
As a final example we note that the statement B=A(:) in the first example in this lesson could have been done using a for loop (which we will talk about later) and the empty matrix as follows
y=[] for j=1:n, y=[y;A(:,j)],end %we will get to loops later
x=0:.05:2*pi; y1=sin(x); y2=cos(x); plot(x,y1) %this is the basic syntax for plotting a function % f(x) at the values of a vector x. The graph is generated by linear %interpolation of the values of (x(j),f(x(j))) hold on % this freezes the current axes for additional plotting pause % stop program execution until you hit any key plot(x,y2) % plot another function on the same axes hold off % release the axis so that future plots generate new axes
x=0:.05:2*pi; y1=sin(x); y2=cos(x); plot(x,y1,x,y2)
You should look at the plot command in the help file since there are many, many commands that can be given to the plot statement. For example, to plot values y_j=f_j(x_j) for j=1, ...,n where each x_j is a vector, use the syntax plot(x_1,y_1,x_2,y_2,...,x_n,y_n). If the vectors x_j are the same column vector, say x=x_1= ... x_n, and the y_j=f_j(x), you can build the matrix Y=[y_1 y_2 ... y_n] and issue the command plot(x,Y).
x=(0:.05:1)'; Y=(x*ones(1,5)).^(ones(size(x))*(1:5)); % this gives a matrix whose columns are x^j, j=1..5 plot(x,Y)
x=0:.05:2*pi; y1=sin(x); y2=cos(x); h1=figure plot(x,y1,x,y2) h2=figure Y=[2*y1.*y2; sin(2*x)]; % Note the Hadamard operation in the first term plot(x,Y) % Notice we only have one graph since sin(2x) = 2 sin(x) cos(x)? h3=figure x=-2.5:.05:2.5; y=x.^2.*(4-x.^2); plot(x,y) pause % we insert a pause command to suspend program operation close all % the close command is used to close figure windows, %close all simply closes all figure windows
A=[1 2 3 4 5 6 7 8 9;1 1 1 1 1 1 1 1 1;0 0 0 0 0 0 0 0 0; ... 9 8 7 6 5 4 3 2 1;-1 -1 -1 -1 -1 -1 -1 -1 -1;2 1 2 1 2 1 2 1 1]
v=[1 2 3 4 5] u=v(5:-1:1)
You might also look at help fliplr.
M=[1 0 1 0 1;1 1 1 1 1;-2 -2 -2 -2 -2;1 2 3 4 5] M(4,:)=-M(2,:)
v([1 3 5])=1:3
and the values v(2) and v(4) are automatically set equal to 0.
x1=.01:.0025:.25; x2=[x1 .3:.025:.5];