Lab # 1 Title: Introduction To Matlab: Task 1
Lab # 1 Title: Introduction To Matlab: Task 1
Task 1:
Q1. Create a vector x containing integer numbers from 1 to 100.
>> x=1:100
Task 2:
Q2. From x create y containing first 25 elements of x, z containing
elements of x with indexes from 50 to 75 and w containing elements with
even indexes
clear all
close all
clc
x=[1:100]
y=[1:x:25]
z=[50:x:75]
evens=x(mod(x,2)==0)
Task 3:
Create matrix 3 by 3 with all ones. Create matrix 8 by 1 with all zeros.
Create matrix 5 by 2 with all elements equal to 0.37.
>> ones(3,3)
ans =
1 1 1
1 1 1
1 1 1
>> zeros(8,1)
ans =
0
0
0
0
0
0
0
0
>> ones(5,2)
ans =
1 1
1 1
1 1
1 1
1 1
>> c=0.37*ones(5,2)
c=
0.3700 0.3700
0.3700 0.3700
0.3700 0.3700
0.3700 0.3700
0.3700 0.3700
Task 4:
Q4. Create vector 1 by 25 containing random elements uniformly
distributed in the interval [0.5, 0.5].
>> rand(1,25)
ans =
Columns 1 through 13
Columns 14 through 25
>> c=rand(1,25)-0.5
c=
Columns 1 through 13
Columns 14 through 25
Task 5:
Create a vectorx= [3,1,2,5,4]. From x create y containing the same
elements in the reverse order, find indices of elements greater than 2,
create z containing elements of x which are smaller than 4.
>> x=[3,1,2,5,4]
x=
3 1 2 5 4
>> y=flip(x)
TASK NO 7
Q7. Given matrix m = [1, 2, 3; 2, 1, 5; 4, 6, 4; 2, 3, 2], create its submatrix
n containing first two rows and the first and the third column (i.e., row
indexes I =1, 2 and column indexes j
=1, 3).
clear all
close all
clc
A = [ 1 2 3;2 1 5;4 6 4;2 3 2 ]
b=A([1,2],[1,3])
A=
1 2 3
2 1 5
4 6 4
2 3 2
b=
1 3
2 5
TASK NO 8
Q8. Given the same matrix m = [1,2,3;2,1,5; 4,6,4;2,3,2], create matrix n
with rows sorted in a descending order of elements in the second column.
Example: 1 2 3 4 6 4
2 1 5 => 2 3 2
464123
232215
clear all
close all
clc
m=[1 2 3;2 1 5;4 6 4;2 3 2]
sortrows(m,[-2 -2])
TASK NO 9:
Q9. Calculate the outer product of two vectors x = [1,2,3] and y=
[0.1,0.2,0.3]. Multiply these two vectors element by element.
clear all
close all
clc
x=[1 2 3]
y=[0.1 0.2 0.3]
c=x.*y
TASK NO 10:
Q10. Given vector a= [8, 6, 4] and integer number n= 4 create matrix b
containing n-times a(1) in the first row, n-times a(2) in the second row,
etc. (i.e. b= [8,8,8,8; 6,6,6,6; 4,4,4,4]).
clear all
close all
clc
x=[8;4;6]; n=4;
newx = [repmat(x(1),1,n),repmat(x(2),1,n),repmat(x(3),1,n)]