0% found this document useful (0 votes)
2 views16 pages

part2_ Variables in MATLAB (1)

The document provides an overview of predefined size functions and element extraction/indexing in MATLAB, illustrating how to create matrices and access their elements using various methods. It also covers operations such as elementwise arithmetic, algebraic operations, and logical comparisons. Additionally, it includes examples of modifying matrix elements and using delete operators.

Uploaded by

wailmeskine74
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views16 pages

part2_ Variables in MATLAB (1)

The document provides an overview of predefined size functions and element extraction/indexing in MATLAB, illustrating how to create matrices and access their elements using various methods. It also covers operations such as elementwise arithmetic, algebraic operations, and logical comparisons. Additionally, it includes examples of modifying matrix elements and using delete operators.

Uploaded by

wailmeskine74
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Predifined size functions

A=[1:4;linspace(1,8,4)] A=ones(1,3)
ans = ans =
>>numel(A) 8 3
ans = ans =
>>length (A) 4 3
R= R=
>> R=size(A) 2 4 1 3
n= m= n= m=
>>[n,m]=size(A) 2 4 1 3
ans = ans =
>>size(A,1) 2 1
ans = ans =
>>size(A,2) 4 3
ans = ans =
>>ndims(A) 2 2
Elements extraction/indexing
• variable_name(linear index)
• Variable_name(row,column)
A=[1:4;linspace(1,8,4)] A=ones(1,3)
A=
A(0)
1.00 2.00 3.00 4.00 A=1 1 1
1.00 3.33 5.66 8.00
>>A(3) ans = ans =
2.00 1
ans =
>>A(2,3) 5.66
error

ans = ans=
>>A(end) 8 1
Elements extraction /indexing
A=[1:4;linspace(1,8,4)] A=ones(1,3)
A=
1.00 2.00 3.00 4.00 A=1 1 1
1.00 3.33 5.66 8.00
3.33
ans =
3.00
>>A(:) 1.00 ans =
5.66
1.00 111
4.00
2.00
8.00
ans = 3.00
>>A(:,3) 1
5.66
>>A(1, [2 4])
ans = 2.00 4.00 error
>>A(1,2:2:4)
ans =
>> A(1:3) 1 1 3 1 1 1
Elements extraction
Logical search
Elements extraction/ indexing
A=[1:4;linspace(1,8,4)] A=ones(1,3)
A=
A(0)
1.00 2.00 3.00 4.00 A=1 1 1
1.00 3.33 5.66 8.00
A=
>>A(3)=-5
1.00 -5 3.00 4.00 A=1 1 -5
1.00 3.33 5.66 8.00
A=
A=1 1 –3
>>A(1,3)=-3 1.00 -5 -3 4.00
1.00 3.33 5.66 8.00
A=
1.00 -5 -3 4.00
A(end,end)=0 1.00 3.33 5.66 0
A=1 1 0
Elements extraction/ indexing
A=[1:4;linspace(1,8,4)] A=ones(1,3)
A=
1.00 2.00 3.00 4.00 A=1 1 1
1.00 3.33 5.66 8.00
A(:)=5 A= 5 5 5 5
A= 5 5 5
5 5 5 5
A=
A(:,3)=0 1.00 2.00 0 4.00 000
1.00 3.33 0 8.00

A(1, 6) error error

1.00 2.00 0 4.00 0 -1.00


A(1,6)=-1 1 1 1 0 0 1
1.00 3.33 0 8.00 0 0
Delete operator
A=
>>A=[]
1.00 2.00 3.00 4.00
>>A(row, column)=[]
1.00 3.33 5.66 8.00
>> A(2)=[]
A=
1.00 2.00 3.33 3.00 5.66 4.00 8.00

>> A(2,3)=[]  error

>>A(:, 2) =[]
A=
1.0000 3.0000 4.0000
1.0000 5.6667 8.0000
Functions /Operators
Elementwise operations
a=1:4  identical size of a and
b
b=10:10:40

>> a+b  >> plus(a,b)


ans =
11.00 22.00 33.00 44.00
>> a-b
ans = >> b.^a
-9.00 -18.00 -27.00 -36.00 ans =
10 400 27000 2560000
>> a.*b  >>times(a,b)
ans =
10.00 40.00 90.00 160.00
>> a./b  >>rdivide(a,b)
ans =
0.10 0.10 0.10 0.10
Algebria operations
• mtimes  size(A)

• mrdivide, mldivide  solves the system of


linear equations : x.A=B , A.x=B (algebria)
• mpower  A^2 is equivalent to A*A.
2/A (algebria)
Algebria operations
• mtranspose, transpose 
Scalar/array
operations
Functions /Operators
Functions /Operators

>>help ops
Functions /Operators
>> 5<3 >> E= 3 & 7
ans = E=
logical logical
0 1
>> 5>-1 >>R= 0 |0
ans = R=
logical logical
1 0
1:3==[1 2 4] >> xor([1 1 0 0],[1 0 1 0])
ans = ans =
1×3 logical array 1×4 logical array
1 1 0 0 1 1 0

You might also like