MATLAB MONDAYS💥- Crash Course part-4

Aatmaj - Jul 19 '21 - - Dev Community

Welcome all! ❤️‍🔥 This Monday let us learn all about matrices in MATLAB, and how to generate them🤟


Making matrices in MATLAB
We will now learn how to create Matrices in MATLAB.
Similer to row vectors, the elements in a row are separated by a ',' and column by a ';'. First, type in the values for the first column separated by commas. When the row ends, type a semicolon and the proceed on to the next row. Repeat until done, and end with the square brackets.

image

Mismatch of elements in rows or columns generates this error-
image

Fusing two Matrices
In a similar way of making matrices, we can combine them together with this syntax

a=[  ]
b=[  ]
c=[a;b]
Enter fullscreen mode Exit fullscreen mode

This is vertical concatenation. The two matrices will be placed one on top of the other and joined together

For horizontal concatenation, use the syntax c=[a,b]

image

Note, For horizontal Concatenation, the number of rows for both matrices must be the same, and for vertical concatenation, the number of columns must be equal. If this rule is violated, Concatenation error is generated.

Matrix generation functions
There are a few matrix generation functions in MATLAB like eye() this function generates an Identity matrix of the size we input.
example-

>> I=eye(10)

I =

     1     0     0     0     0     0     0     0     0     0
     0     1     0     0     0     0     0     0     0     0
     0     0     1     0     0     0     0     0     0     0
     0     0     0     1     0     0     0     0     0     0
     0     0     0     0     1     0     0     0     0     0
     0     0     0     0     0     1     0     0     0     0
     0     0     0     0     0     0     1     0     0     0
     0     0     0     0     0     0     0     1     0     0
     0     0     0     0     0     0     0     0     1     0
     0     0     0     0     0     0     0     0     0     1
Enter fullscreen mode Exit fullscreen mode

The zeros() function creates a square martix of zeros
For rectangular matrices, two arguments can be used.

>> z=zeros(3)

z =

     0     0     0
     0     0     0
     0     0     0

>> z=zeros(3,5)

z =

     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
Enter fullscreen mode Exit fullscreen mode

We can use the exact same syntax for the ones function and the rand function.

>> x=rand(5,6)

x =

    0.8147    0.0975    0.1576    0.1419    0.6557    0.7577
    0.9058    0.2785    0.9706    0.4218    0.0357    0.7431
    0.1270    0.5469    0.9572    0.9157    0.8491    0.3922
    0.9134    0.9575    0.4854    0.7922    0.9340    0.6555
    0.6324    0.9649    0.8003    0.9595    0.6787    0.1712

>> x=ones(5,6)

x =

     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
Enter fullscreen mode Exit fullscreen mode

Similarly there is the diag function for a diagonal matrix

>> x=diag(1:7)

x =

     1     0     0     0     0     0     0
     0     2     0     0     0     0     0
     0     0     3     0     0     0     0
     0     0     0     4     0     0     0
     0     0     0     0     5     0     0
     0     0     0     0     0     6     0
     0     0     0     0     0     0     7
Enter fullscreen mode Exit fullscreen mode

That's all for today. For any suggestions or doubts, please comment below 👇 (they do motivate me a lot...) 🎗️ Follow me for updates...
Also, you can gmail me for any suggestion or help 🙌
LinkedIn
Gmail

Bye for now 🖐
Happy to help always.....😃
Meet you all soon👍

➕➖✖️➗

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .