MATLAB MONDAYS💥- Crash Course part-5

Welcome all! ❤️‍🔥 This Monday let us learn about statistical functions and how to calculate on matrices.🤟

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

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

However, when we multiply the two, an error occurs.
image
This is because the 8 operator means matrix multiplication and not element wise multiplication. For element wise multiplication, we use .*. Similar for division ./ and exponentiation .^

>> 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.

Note, the matrix operations are only valid for matrices of same sizes and dimensions. If this is not followed, this error pops up-
image

Here is an example of the application of what we learnt so far-
image
Try plotting a few equations yourself....

Matrix multiplication
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.

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

That's all for this week. 🏆 Your comments really motivate me, so for any suggestions or doubts, please comment below 👇, and I will be happy to help 🙂 🎗️ Follow me for updates...
Also, you can gmail me for any suggestion or help 🙌
LinkedIn
Gmail

Bye for now 🖐
Meet you all soon👍

➕➖✖️➗

24