part1_ Variables in MATLAB (1)
part1_ Variables in MATLAB (1)
1st principal
All MATLAB variables are
multidimensional arrays, no matter what type of
data.
two-dimensional arrays
Array Creation
row vector
• By components listing
>>a= [1 2 3 4]
>>a= [ 1 ,2 ,3 ,4]
a=1234
Array Creation
row vector
• By components description
>>a= 1:4
a=1234
>>a= 1:2:8
a=1357
>>a=1: -1: 9
a=[] %empty
Array Creation
row vector
• MATLAB functions
linspace, logspace, rand, ones, zeros,
>> linspace(initial, final, cpt)
>> linspace(2, 9, 5)
ans =
2.0000 3.7500 5.5000 7.2500 9.0000
>>logspace(2, 9, 5)
ans =
100.00 5623.41 316227.77 17782794.10
1000000000.00
Array Creation
row vector
• Other methods :
– concatenation
>>a=[1 2 3], b=[5 6 7],
>> c=[a, b]
c=
123 567
– Import data
MAT files : >>Load or File menu
Array Creation
column vector
• By components • By components
listing description
>>a= [ 1 ; 2 ; 3 ; 4] >> a=(1:4)’
>>a= ( 1:2:8)’ 1
a= a= 3
1
2 >>a=(1: -1: 9)’ 5
3 7
a=[] %empty
4
Array Creation
column vector
• MATLAB functions • concatenation 1
>> linspace(2, 9, 5)‘ >> a=(1:4)’ 2
3
ans = >>b= ( 1:2:8)’
4
2.0000 >>C=[a ; b]
1
3.7500 3
5.5000 5
7.2500 7
• Import data
9.0000
Array Creation
matrix
• By components listing
>>a= [ 1 2 3 ; 4 5 6 ; 7 8 9]
a=
1 2 3
4 5 6
7 8 9
• By components description
a= [1 : 3 ; 4 : 6 ; 7 : 9]
b=[(1:3:7)', (2:3:8)' , (3:3:9)']
Array Creation
matrix
• MATLAB functions
>> function_name(rows? , columns?)
>> ones(3,5) >>rand(2,4)
>> zeros(3,5) >>randi([-3,2], 2,5)
Square matrices
>>ones(3)
>>zeros(4)
>>rand(2)
Can we create vectors using these functions?
Array Creation
matrix
• Concatenation variable size
convenient arrays size A 2X1
D 2X2
>>C=[Z B; A D F] F 2X4
Z 3X2
B 3X5
C ?
ans ?
>>cat(2,A,F)% [A,F].
ans ?
>>cat(1,D,Z)% [D;Z].
Array Creation
practice
• Create a vector of odd values from 1 to 10
• Create a matrix of 4 rows by 4 columns of
value 3
• Create a matrix of 2x5 random integer values
ranged from 1 to 6
Array Creation
practice
>>A=1 : 2 : 10
>>M=3*ones(4)
>>Z=randi([1,6],2,5)