DEV Community

Cover image for MATLAB MONDAYS💥- Crash Course part-6
Aatmaj
Aatmaj

Posted on • Edited on

2 2

MATLAB MONDAYS💥- Crash Course part-6

Welcome all! ❤️‍🔥 This Monday let us learn about Accessing vector elements 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 access 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 an array of rows while columns is array of columns. Here are two examples that 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👍

➕➖✖️➗

Image of Wix Studio

2025: Your year to build apps that sell

Dive into hands-on resources and actionable strategies designed to help you build and sell apps on the Wix App Market.

Get started

Top comments (0)

Image of Bright Data

Ensure Data Quality Across Sources – Manage and normalize data effortlessly.

Maintain high-quality, consistent data across multiple sources with our efficient data management tools.

Manage Data

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay