EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Matlab ones
 

Matlab ones

Updated March 13, 2023

Matlab ones

 

 

Introduction to Matlab ones

Matlab one’s function is used to get a scalar or a vector with all ‘ones’ (1s) as its elements. Loops like ‘while’ & ‘for’ increase the size of our data structure incrementally every time a loop is executed. This process adversely affects the performance of the program. Resizing arrays repeatedly requires extra time seeking bigger contiguous memory blocks & then placing the arrays into these blocks. Functions like Matlab ones are used to improve code performance by pre-allocating the space required for the input array.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax of Function ‘ones’ in Matlab:

A = ones

A = ones (n)

A = ones (size 1, size 2, size 3, ….)

Description:

  • A = ones will return scalar 1 as output.
  • A = ones (n) will return a square matrix of order n X n.
  • A = ones (size 1, size 2, size 3, ….) will return a matrix of required dimensions.

Examples of Matlab ones

Given below are the examples of Matlab ones:

Example #1

In the first example, we will use ‘one’s function’ to create a square matrix of order 3 with all 1s as its elements.

Below are the steps that we will follow for this example:

  • First, pass the required order as an argument to the one’s function.
  • Display the output.

Code:

A = ones(3)

[Using ‘ones function’ with ‘3’ as the argument. Please note that we have passed ‘3’ as an argument because we need the matrix with 1s to be of order 3 x 3 ]

A

[Displaying the output]

This is how our input and output will look like in the Matlab command window:

Input:

A = ones(3)
A

Output:

Matlab ones 1

As we can see in the output, the function ‘ones’ has given us a matrix of order 3 and with 1s as its elements.

Example #2

In this example, we will use one function to create a square matrix of order 4 with all 1s as its elements.

Below are the steps that we will follow for this example:

  • First, pass the required order as an argument to the one’s function.
  • Display the output.

Code:

A = ones(4)

[Using ones function with ‘4’ as the argument. Please note that we have passed ‘4’ as an argument because we need the matrix with 1s to be of order 4 x 4 ]

A

[Displaying the output]

This is how our input and output will look like in the MATLAB command window.

Input:

A = ones(4)
A

Output:

function to create a square matrix

As we can see in the output, the function ‘ones’ has given us a matrix of order 4 and with 1s as its elements.

In the above 2 examples, we have used ‘ones function’ to create a square matrix of all ones. Next, we will see the syntax to create the matrix of the required order. i.e. a matrix which is not a square matrix.

Example #3

In this example, we will use ‘ones function’ to create a matrix with all 1s as its elements and order 2 x 3 ( 2 rows and 3 columns).

Below are the steps that we will follow for this example:

  • First, pass the required order as an argument to the ones function.
  • Display the output.

Code:

A = ones(2, 3)

[Using ones function with ‘2’ and ‘3’ as the arguments. Please note that we have passed 2, 3 as the arguments because we need the matrix with 1s to be of order 2 x 3 ]

A

[Displaying the output]

This is how our input and output will look like in the MATLAB command window.

Input:

A = ones(2, 3)
A

Output:

Matlab ones 3

As we can see in the output, the function ‘ones’ has given us a matrix of order 2 x 3 and with 1s as its elements.

Example #4

In this example, we will use ‘ones function’ to create a matrix with all 1s as its elements and order 3 x 4 ( 3 rows and 4 columns).

Below are the steps that we will follow for this example:

  • First, pass the required order as an argument to the ones function.
  • Display the output.

Code:

A = ones(3, 4)

[Using ‘ones function’ with ‘3’ and ‘4’ as the arguments. Please note that we have passed 3, 4 as the arguments because we need the matrix with 1s to be of order 3 x 4 ]

A

[Displaying the output]

This is how our input and output will look like in the MATLAB command window:

Input:

A = ones(3,4)
A

Output:

create a matrix with all 1s as its elements and order

As we can see in the output, the function ‘ones’ has given us a matrix of order 3 x 4 and with 1s as its elements.

Conclusion

Ones function is used to get a scalar or a vector with all ‘ones’ (1s) as its elements Matlab ones is used to improving code performance by pre-allocating the space required for the input array. We can use ones function in its different forms to get the order of the matrix as per our requirement.

Recommended Articles

This is a guide to Matlab ones. Here we discuss the introduction to Matlab ones along with appropriate examples, respectively. You may also have a look at the following articles to learn more –

  1. Matlab Annotation
  2. Matlab fwrite
  3. Matlab Image Resize
  4. Convolution Matlab

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - ENROLL NOW