0% found this document useful (0 votes)
47 views7 pages

Lab # 1 Title: Introduction To Matlab: Task 1

The document contains instructions for 10 tasks in MATLAB. The tasks include: creating vectors containing integer sequences and subsets of elements from another vector; generating matrices with specified element values or random values within a range; extracting submatrices and sorting rows of a matrix; performing element-wise vector multiplication; and replicating vector elements to construct a matrix.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views7 pages

Lab # 1 Title: Introduction To Matlab: Task 1

The document contains instructions for 10 tasks in MATLAB. The tasks include: creating vectors containing integer sequences and subsets of elements from another vector; generating matrices with specified element values or random values within a range; extracting submatrices and sorting rows of a matrix; performing element-wise vector multiplication; and replicating vector elements to construct a matrix.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

LAB # 1

Title: Introduction To Matlab

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

0.8147 0.9058 0.1270 0.9134 0.6324 0.0975 0.2785 0.5469


0.9575 0.9649 0.1576 0.9706 0.9572

Columns 14 through 25

0.4854 0.8003 0.1419 0.4218 0.9157 0.7922 0.9595 0.6557


0.0357 0.8491 0.9340 0.6787

>> c=rand(1,25)-0.5

c=

Columns 1 through 13

0.2577 0.2431 -0.1078 0.1555 -0.3288 0.2060 -0.4682 -0.2231


-0.4538 -0.4029 0.3235 0.1948 -0.1829

Columns 14 through 25

0.4502 -0.4656 -0.0613 -0.1184 0.2655 0.2952 -0.3131 -0.0102


-0.0544 0.1463 0.2094 0.2547

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)]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy