


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



















x=[1 3 5 7 9]
x = 1 3 5 7 9
Y=[0, 3+4i; 1-i,2]*
Y = 0 3.0000 + 4.0000i 1.0000 - 1.0000i 2.
a=1; b=2; c=3; d=4; e=5;, f=6; Z=[a a+b a-c; d e+2f e^b]*
x=[1 3 5 7 9] % Create array x y=x-5 % Subtract 5 from each element in x and store result in array y
x = 1 3 5 7 9 y = -4 -2 0 2 4
z=x+y % Add arrays x and y element by element
z = -3 1 5 9 13
z=5z + 1 % Multiply all elements in array z by 5 and then add 1 to % each element and store result in array z*
z = -14 6 26 46 66
theta=[0 pi/4 pi/3 pi/2; pi 5pi/4 4pi/3 3pi/2] r=cos(theta) % Compute cosine of all elements in array theta and store % result in array r*
x = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
y = 2 3 4
x % Display array x z=x(2:end,3) % Selects elements x(2,3),x(3,3),x(4,3)
x = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
z = 8 13 18
B=A(2,1:2:7) % Selects elements A(2,1),A(2,3),A(2,5),A(2,7)
A % Display A C=A(3,1:3:10) % Selects elements A(3,1),A(3,4),A(3,7),A(3,10)
A = 1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1 0 2 4 6 8 10 12 14 16 18
C = 0 6 12 18
A % Display A D=A(1,10:-2:4) % Selects elements A(1,10),A(1,8),A(1,6),A(1,4)
A = 1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1 0 2 4 6 8 10 12 14 16 18
x=0:5:
x = Columns 1 through 12 0 5 10 15 20 25 30 35 40 45 50 55 Columns 13 through 21 60 65 70 75 80 85 90 95 100
y=10:-2:-
y = 10 8 6 4 2 0 -2 -4 -6 -8 -
z=25:0.5:
z = Columns 1 through 7 25.0000 25.5000 26.0000 26.5000 27.0000 27.5000 28. Columns 8 through 11 28.5000 29.0000 29.5000 30.
a=15; t=a:a+
t = 15 16 17 18 19 20
x=[ 1 2 10:2:20 30]
x = 1 2 10 12 14 16 18 20 30
y=[1:10; 0 0 50:2:64]
y = 1 2 3 4 5 6 7 8 9 10 0 0 50 52 54 56 58 60 62 64
y=[1;2;3;4;5;6;7] % Create a 7 ×× 1 array or column vector
y = 1 2 3 4 5 6 7
x=1:7 % Create row vector x x=x' % Transpose row vector x to column vector x
x = 1 2 3 4 5 6 7
x = 1 2 3 4 5 6 7
A=[1:4;5:8;9:12] % Create 3 ×× 4 array A
B=A' % Transpose A into 4 ×× 3 array B by swapping rows and columns of A
x=[1+2i, 3i, 5, 4-9i] y=x'*
x = 1.0000 + 2.0000i 0 + 3.0000i 5.0000 4.0000 - 9.0000i
y = 1.0000 - 2.0000i 0 - 3.0000i
4.0000 + 9.0000i
x=[12-i; 3; 2+5i;] y=x.'*
x = 12.0000 - 1.0000i
2.0000 + 5.0000i
y = 12.0000 - 1.0000i 3.0000 2.0000 + 5.0000i
x=[1+i,1-i; 2+3i,5-2i; 4i,8+2i] y=x' % Transpose and conjugate x z=x.' % Transpose x**
x = 1.0000 + 1.0000i 1.0000 - 1.0000i 2.0000 + 3.0000i 5.0000 - 2.0000i 0 + 4.0000i 8.0000 + 2.0000i
y = 1.0000 - 1.0000i 2.0000 - 3.0000i 0 - 4.0000i 1.0000 + 1.0000i 5.0000 + 2.0000i 8.0000 - 2.0000i
z = 1.0000 + 1.0000i 2.0000 + 3.0000i 0 + 4.0000i 1.0000 - 1.0000i 5.0000 - 2.0000i 8.0000 + 2.0000i
??? Error using ==> ^ Matrix must be square.
C=2^A
??? Error using ==> ^ Matrix must be square.
A=[1 2 3; 4 5 6; 7 8 9; 10 11 12] % A is 4 ×× 3
B=[12 11 10; 9 8 7; 6 5 4; 3 2 1] % B is 4 ×× 3
??? Error using ==> * Inner matrix dimensions must agree.
C=A.B*
A=ones(2,3) % Create 2 ×× 3 array of all 1's
y=zeros(1,5) % Create 1 ×× 5 array of all 0's
y = 0 0 0 0 0
B=ones(4) % Create 4 ×× 4 array of all 1's
C=zeros(3) % Create 3 ×× 3 array of all 0's
C = 0 0 0 0 0 0 0 0 0
D=[ones(3) zeros(3); zeros(3) ones(3)]% Create 6 ×× 6 array with sub-arrays
I=eye(3) % Create 3 ×× 3 Identity matrix
I24=eye(2,4) % Create 2 ×× 4 array with embedded 2 ×× 2 Identity matrix
I42=eye(4,2) % Create 4 ×× 2 array with embedded 2 ×× 2 Identity matrix
x=rand(1,7) % Create 1 ×× 7 row vector of 7 U(0,1) random numbers y=10+(15-10)x % Transform x to array of 7 U(10,15) random numbers*
x = 0.1341 0.0653 0.3751 0.3735 0.4840 0.9695 0. y = 10.6704 10.3266 11.8757 11.8676 12.4201 14.8473 11.
A=rand(4) % Create 4 ×× 4 array of U(0,1) random numbers
A = 0.2527 0.4864 0.8578 0. 0.5849 0.4961 0.6098 0. 0.5237 0.8432 0.5657 0. 0.1634 0.8062 0.6119 0.
y=randn(10,1) % Create 10 ×× 1 column vector of N(0,1) Normally % distributed random numbers z=50+3y % Transform y to 10 ×× 1 column vector of N(50,3) Normally % distributed random numbers*
y = -0. -1.
-1.
-0.
z = 48.
clear A(3,3)=0 % Create 3 ×× 3 array A and fill with 0's A(2,2)=5 % Change element A(2,2) to 5 A(1,3)=-1 % Change element A(1,3) to - A(4,5)=10 % Expand A to a 4×5 array and set value of A(4,5) to 10
A = 0 0 0 0 0 0 0 0 0
A=diag(0:4) % Create a 5 ×× 5 diagonal array with elements 0,1,2,3, A(3,:)=0 % Set the 3rd row of A equal to all zeros A(2:4,5)=-8 % Set A(2,5), A(3,5) and A(4,5) equal to -
B=[1:5;6:10;11:15;16:20;21:25] % Create 5 ×× 5 array B B(2:3,4:5)=0 % Set elements B(2,4), B(2,5), B(3,4), B(3,5) to zero
C=B(:,[1 3 5]) % Set C equal to 5 ×× 3 array made up of the 1st, 3rd % and 5th columns of array B
D=C(3:4,2:3) % Set D equal to 2 ×× 2 array using elements in 3rd and % 4th rows, and 2nd and 3rd columns of C
E=C(:) % Create a 15 ×× 1 array E made up of the 3 columns of array C
A=[1:4;8:-1:5; 0 -3 9 12] % Create 3×4 array A x=A(3) % Finds the A(3,1) element of array A y=A(7) % Finds the A(1,3) element of A z=A(end) % Finds the A(3,4) element of array A u=A(5:10) % Finds elements A(2,2),A(3,2),A(1,3),A(2,3),A(3,3),A(1,4)
x = 0
y = 3
z = 12
u = 7 -3 3 6 9 4
x=100+10randn(1,7) % Generate array of random numbers ~ N(100,10) xgt98=x>98 % Create logical array xgt98 same size as x with 1's (True) % in positions corresponding to wherever element in x > % and 0's (False) where element in x <= y=x(xgt98) % Find the elements in array x which are > 98*
x = 112.5400 84.0627 85.5904 105.7115 96.0011 106.9000 108. xgt98 = 1 0 0 1 0 1 1 y = 112.5400 105.7115 106.9000 108.
x=1:2:15 % Create 1 ×× 8 array x a=[2 5 7] % Create index vector 'a' w=x(a) % Address x using index vector 'a' and store in 1 by 3 array w
x = 1 3 5 7 9 11 13 15 a = 2 5 7 w = 3 9 13
x36=(x>=3 & x<=6)% Create 1 ×× 8 logical array 'x36' with 1's where % elements in x are betwen 3 and 6 and 0's elsewhere y=x(x36) % Find elements (numerical values) in x between 3 and 6
x36 = 0 1 1 0 0 0 0 0 y = 3 5
b=[1 0 1 0 1 0 0 0] % Create 1 ×× 8 numerical array b x=0:5: %z=x(b) % Invalid MATLAB statement because b is not a logical array Lb=logical(b) % Convert numerical array b to a logical array Lb z=x(Lb) % Extracts the 1st, 3rd and 5th elements of array x
b = 1 0 1 0 1 0 0 0 x = 0 5 10 15 20 25 30 35 Lb = 1 0 1 0 1 0 0 0 z = 0 10 20