DEV Community

loading...
Cover image for MATLAB MONDAYS💥- Crash Course part-6

MATLAB MONDAYS💥- Crash Course part-6

Aatmaj
I'm a Computer Science enthusiast with a keen interest in Maths. I like to tinker with things around me & build projects on my own. I like to play musical instruments & practise Sahaja Yoga meditation
・3 min read

Welcome all! ❤️‍🔥 This Monday let us learn pre-baked MATLAB functions which we can just use off the shelf. After that we will learn the loop in MATLAB.🤟


Many times, we need to access the elements in middle of a vector or matrix. MATLAB provides easy methods to extract out the elements.

Accessing vector elements-

We can access and the vector elements as shown in the example

>> x=[1,2,3,4,0,6];
>> x(5)

ans =

     0

>> x(5)=5;
>> x

x =

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

MATLAB also allows us to access many elements at once. The result is a 'n' element vector

>> x=[2,4,6,8];
>> y=x([2,3])

y =

     4     6
>> y=x([1,3,4])

y =

     2     6     8
Enter fullscreen mode Exit fullscreen mode

An alternative may be

>> x=[2,4,6,8];
>> data=1:3;
>> y=x(data)

y =

     2     4     6
Enter fullscreen mode Exit fullscreen mode

We can acess the last element of thee vector using the "end" keyword.

>> x=[2,4,6,8];
>> x(end)

ans =

     8

>> x(end-1)

ans =

     6
Enter fullscreen mode Exit fullscreen mode

Accessing matrix values-
Matrix values require both the row and the column number to be accessed

>> x=rand(5,6)

x =

    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
    0.0975    0.1576    0.1419    0.6557    0.7577    0.7060

>> x(2,3)

ans =

    0.9572
Enter fullscreen mode Exit fullscreen mode

We can also extract a whole matrix using the syntax
x(rows,columns), where rows is a array of rows while columns is array of columns. Here is are two examples which will make things very clear.
image

Note that 2:4 means [2,3,4] while comma separated 2,4 means [2,4]
image

In order to take an entire column or row, we can just place a semicolon ':' for it. Here is an example-

>> x=rand(5,5)

x =

    0.8314    0.4168    0.0155    0.1981    0.0527
    0.8034    0.6569    0.9841    0.4897    0.7379
    0.0605    0.6280    0.1672    0.3395    0.2691
    0.3993    0.2920    0.1062    0.9516    0.4228
    0.5269    0.4317    0.3724    0.9203    0.5479

>> x(:,3)

ans =

    0.0155
    0.9841
    0.1672
    0.1062
    0.3724

>> x(3,:)

ans =

    0.0605    0.6280    0.1672    0.3395    0.2691
Enter fullscreen mode Exit fullscreen mode

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👍

➕➖✖️➗

Discussion (0)