28
MATLAB MONDAYS💥- Crash Course part-5
Matrix calculations
We may at times require to manipulate the individual elements of matrices. We can make these matrix calculations similar to variable operations.
Example we want to make a matrix y such that each element in y is twice that of each in x. We can do so by y=2*x
We may at times require to manipulate the individual elements of matrices. We can make these matrix calculations similar to variable operations.
Example we want to make a matrix y such that each element in y is twice that of each in x. We can do so by y=2*x
>> x=[1,2,3,4,5];
>> y=2*x
y =
2 4 6 8 10
Now, if we want to make another vector z which is the addition in x and y, z=x+y
>> z=x+y
z =
3 6 9 12 15
>> z=x-y
z =
-1 -2 -3 -4 -5
>> z=y.*x
z =
2 8 18 32 50
>> z=x./y
z =
0.5000 0.5000 0.5000 0.5000 0.5000
>> z=x.^y
z =
1 16 729 65536 9765625
When dealing with two matrices, always add a dot before operators for making element wise operators .*
, ./
and .^
. This is because normal operators are reserved for matrix operations.
Matrix multiplication
Matrix multiplication can be done easily by just multiplying the two together. However, be careful that the dimensions for matrix multiplication match.
Matrix multiplication can be done easily by just multiplying the two together. However, be careful that the dimensions for matrix multiplication match.
>> x=[1,2,3;4,5,6]
x =
1 2 3
4 5 6
>> y=[1,2;3,4;5,6]
y =
1 2
3 4
5 6
>> x*y
ans =
22 28
49 64
Statistical functions
Many times we need to find mean, mode or median of a vector. Such a statistical analysis of the data can be done easily in MATLAB using inbuilt functions.
The mean() function can find the mean of the vector elements, while median() returns the central number.
The mode() gives out the mode of the vector, while var() and std() return the variance and the standard deviations respectively. rms() indicated the root mean square.
Apart from these widely used functions, you will find many more in the MATLAB documentation.
Many times we need to find mean, mode or median of a vector. Such a statistical analysis of the data can be done easily in MATLAB using inbuilt functions.
The mean() function can find the mean of the vector elements, while median() returns the central number.
The mode() gives out the mode of the vector, while var() and std() return the variance and the standard deviations respectively. rms() indicated the root mean square.
Apart from these widely used functions, you will find many more in the MATLAB documentation.
>> x=[1,2,3,4,5,4,3,2,1,1];
>> mean(x)
ans =
2.6000
>> mode(x)
ans =
1
>> var(x)
ans =
2.0444
>> std(x)
ans =
1.4298
>> median(x)
ans =
2.5000
>> y=median(x).*x
y =
2.5000 5.0000 7.5000 10.0000 12.5000 10.0000 7.5000 5.0000 2.5000 2.5000
>> rms(x)
ans =
2.9326
Bye for now 🖐
Meet you all soon👍
Meet you all soon👍
➕➖✖️➗
28