0% found this document useful (0 votes)
14 views14 pages

Ihsan Ghani

The document contains details of various operations performed on vectors in MATLAB. It defines several vectors like V1, V2, V3 etc. and performs element-wise operations like addition, subtraction, multiplication, power, logical operations and functions like any() and all() on them. Various parts of vectors are extracted using indexing and one element of a vector is changed.

Uploaded by

ihsanghani7676
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)
14 views14 pages

Ihsan Ghani

The document contains details of various operations performed on vectors in MATLAB. It defines several vectors like V1, V2, V3 etc. and performs element-wise operations like addition, subtraction, multiplication, power, logical operations and functions like any() and all() on them. Various parts of vectors are extracted using indexing and one element of a vector is changed.

Uploaded by

ihsanghani7676
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/ 14

NAME : Ihsan Ghani

Roll No : 23-CSE-10

Department : Computer Systems Engineering

Subject: Linear Algebra

Lab: 1 Introduction to Matlab


1. Creating vectors
(solution)

A=[1 0 4 5 3 9 0 2]

A =

1 0 4 5 3 9 0 2

a=[4 5 0 2 0 0 7 1]

a =

4 5 0 2 0 0 7 1

(b) Generate the following vectors

B=[A a]

B =

1 0 4 5 3 9 0 2 4 5 0 2 0
0 7 1

C=[a A]
C =

4 5 0 2 0 0 7 1 1 0 4 5 3
9 0 2
D = [0 0 0 . . . 0] with fifty 0’s

D=zeros(10,5)

D =

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
E = [1 1 1 . . . 1] with a hundred 1’s
E=ones(10,10)

E =

1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
F = [1 2 3 4 . . . 30]

F=[1:30]

F =

1 2 3 4 5 6 7 8 9 10 11 12 13
14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30
G = [25 22 19 16 13 10 7 4 1]

G=[25:3:1]

G =

1x0 empty double row vector


H = [0 0.2 0.4 0.6 . . . 2.0]
Solution :

H=[0:0.2:2.0]

H =

0 0.2000 0.4000 0.6000 0.8000 1.0000 1.2000


1.4000 1.6000 1.8000 2.0000

1. Operate with the vectors .

V1 = [1 2 3 4 5 6 7 8 9 0]

Solution:
V1=[1:9 0]

V1 =

1 2 3 4 5 6 7 8 9 0

V2=[0.3 1.2 0.5 2.1 0.1 0.4 3.6 4.2 1.7 0.9]

V2 =

0.3000 1.2000 0.5000 2.1000 0.1000 0.4000 3.6000


4.2000 1.7000 0.9000

V3=[4 4 4 4 3 3 2 2 2 1]

V3 =

4 4 4 4 3 3 2 2 2 1

V1=V2+V3

V1 =

4.3000 5.2000 4.5000 6.1000 3.1000 3.4000 5.6000


6.2000 3.7000 1.9000
b) How to get the value of the fifth element of each vector

V1(5)

ans =

3.1000

V2(5)

ans =

0.1000

V3(5)

ans =

What happens if we execute the command V1(0) and V1(11)?

V1(0)

Array indices must be positive integers or logical values.

V1(11)
Index exceeds the number of array elements. Index must not exceed 10.
V5=[0.4 3.6 4.2 1.7 0.9]

V5=[0.4 3.6 4.2 1.7 0.9]

V5 =

0.4000 3.6000 4.2000 1.7000 0.9000

V6=V2*[0.3:0.1 0.4:0.9]

V6 =

0.1200 0.4800 0.2000 0.8400 0.0400 0.1600 1.4400


1.6800 0.6800 0.3600

V6=[0.3 1.2 0.5 2.1 0.1 3.6 4.2 1.7 0.9]

V6 =

0.3000 1.2000 0.5000 2.1000 0.1000 3.6000 4.2000


1.7000 0.9000

V4=V2(1:5)

V4 =

0.3000 1.2000 0.5000 2.1000 0.1000


V5=V2(5:10)

V5 =

0.1000 0.4000 3.6000 4.2000 1.7000 0.9000

V6=V2;

V6(:,6)

ans =

0.4000

V7=V2;

V7(:,7)=[1.4]

V7 =

0.3000 1.2000 0.5000 2.1000 0.1000 0.4000 1.4000


4.2000 1.7000 0.9000

V8=V2;

V8([1 3 5 7 9])%
ans =

0.3000 0.5000 0.1000 3.6000 1.7000

V1+5

ans =

9.3000 10.2000 9.5000 11.1000 8.1000 8.4000 10.6000


11.2000 8.7000 6.9000

V1+V2

ans =

4.6000 6.4000 5.0000 8.2000 3.2000 3.8000 9.2000


10.4000 5.4000 2.8000

V1-V3

ans =

0.3000 1.2000 0.5000 2.1000 0.1000 0.4000 3.6000


4.2000 1.7000 0.9000

V1.*V2
ans =

1.2900 6.2400 2.2500 12.8100 0.3100 1.3600 20.1600


26.0400 6.2900 1.7100

V1*V2

Error using *
Incorrect dimensions for matrix multiplication. Check that the number of
columns in the first matrix matches the number of rows in the second matrix.
To operate on each element of
the matrix individually, use TIMES (.*) for elementwise multiplication.

Related documentation

V1.^2

ans =

18.4900 27.0400 20.2500 37.2100 9.6100 11.5600 31.3600


38.4400 13.6900 3.6100

V1,^V3

V1,^V3

Invalid use of operator.

V1.^V3
ans =

1.0e+03 *

0.3419 0.7312 0.4101 1.3846 0.0298 0.0393 0.0314


0.0384 0.0137 0.0019

V1^V3

Error using ^
Incorrect dimensions for raising a matrix to a power. Check that the matrix is
square and the power is a scalar. To operate on each element of the matrix
individually, use POWER
(.^) for elementwise power.

V1==V3

ans =

1x10 logical array

0 0 0 0 0 0 0 0 0 0

V1>6

ans =

1x10 logical array

0 0 0 1 0 0 0 1 0 0
V3-(V1-2)

ans =

1.7000 0.8000 1.5000 -0.1000 1.9000 1.6000 -1.6000 -


2.2000 0.3000 1.1000

(V1>2)&(V1<6)

ans =

1x10 logical array

1 1 1 0 1 1 1 0 1 0

(V1>2)|(V1<6)

ans =

1x10 logical array

1 1 1 1 1 1 1 1 1 1

any(V1)

ans =

logical

1
all(V1)

ans =

logical

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