Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Numpy exercise
  • pandas
  • Matplotlib
  • Data visulisation
  • EDA
  • Machin Learning
  • Deep Learning
  • NLP
  • Data science
  • ML Tutorial
  • Computer Vision
  • ML project
Open In App
Next Article:
numpy.diag() in Python
Next article icon

Python | Numpy matrix.diagonal()

Last Updated : 12 Apr, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report

With the help of Numpy matrix.diagonal() method, we are able to find a diagonal element from a given matrix and gives output as one dimensional matrix.

Syntax : matrix.diagonal()

Return : Return diagonal element of a matrix

Example #1 :
In this example we can see that with the help of matrix.diagonal() method we are able to find the elements in a diagonal of a matrix.




# import the important module in python
import numpy as np
         
# make matrix with numpy
gfg = np.matrix('[6, 2; 3, 4]')
         
# applying matrix.diagonal() method
geeks = gfg.diagonal()
   
print(geeks)
 
 
Output:
  [[6 4]]  

Example #2 :




# import the important module in python
import numpy as np
         
# make a matrix with numpy
gfg = np.matrix('[1, 2, 3; 4, 5, 6; 7, 8, 9]')
         
# applying matrix.diagonal() method
geeks = gfg.diagonal()
   
print(geeks)
 
 
Output:
  [[1 5 9]]  


Next Article
numpy.diag() in Python

J

Jitender_1998
Improve
Article Tags :
  • Python
  • Python numpy-Matrix Function
  • Python-numpy
Practice Tags :
  • python

Similar Reads

  • numpy.matrix() in Python
    This class returns a matrix from a string of data or array-like object. Matrix obtained is a specialised 2D array. Syntax : numpy.matrix(data, dtype = None) : Parameters : data : data needs to be array-like or string dtype : Data type of returned array. Returns : data interpreted as a matrix # Pytho
    1 min read
  • numpy.diagflat() in Python
    numpy.diagflat (a, k = 0): Create a two-dimensional array with the array_like input as a diagonal to the new output array. Parameters : a : array_like input data with diagonal elements strong>k : [int, optional, 0 by default] Diagonal we require; k>0 means diagonal above main diagonal or vice
    1 min read
  • numpy.diag() in Python
    numpy.diag(a, k=0) : Extracts and construct a diagonal array Parameters : a : array_like k : [int, optional, 0 by default] Diagonal we require; k>0 means diagonal above main diagonal or vice versa. Returns : ndarray Python Code # Python Programming illustrating # numpy.diag method import numpy as
    1 min read
  • Python | numpy.fill_diagonal() method
    With the help of numpy.fill_diagonal() method, we can get filled the diagonals of numpy array with the value passed as the parameter in numpy.fill_diagonal() method. Syntax : numpy.fill_diagonal(array, value) Return : Return the filled value in the diagonal of an array. Example #1 : In this example
    1 min read
  • Numpy matrix.I function | Python
    With the help ofnumpy.matrix.I() function we can get the multiplicative inverse of the same size as of our given matrix. Syntax : numpy.matrix.I() Return : [matrix object] If self is non-singular, ret is such that ret * self == self * ret == np.matrix(np.eye(self[0, :].size) all return True. Return
    1 min read
  • numpy.matrix.A() function - Python
    numpy.matrix.A() function return self as an ndarray object. Syntax : numpy.matrix.A() Parameters : None Return : [ndarray] Return self as an ndarray. Code #1 : # Python program explaining # numpy.matrix.A() function # importing numpy as geek import numpy as geek mat = geek.matrix(geek.arange(9).resh
    1 min read
  • numpy.asmatrix() in Python
    numpy.asmatrix(data, dtype = None) Returns a matrix by interpreting the input as a matrix. Parameters : data : array-like input data dtype : Data type of returned array Returns : Interprets the input as a matrix # Python Programming illustrating # numpy.asmatrix import numpy as geek # array-like inp
    1 min read
  • Python | Numpy numpy.matrix.all()
    With the help of Numpy numpy.matrix.all() method, we are able to compare each and every element of one matrix with another or we can provide the axis on the we want to apply comparison. Syntax : numpy.matrix.all() Return : Return true if found match else false Example #1 : In this example we can see
    1 min read
  • Python | Numpy numpy.matrix.A()
    With the help of Numpy numpy.matrix.A() method, we can get the same matrix as self. It means through this method we can get the identical matrix. Syntax : numpy.matrix.A() Return : Return self matrix Example #1 : In this example we can see that with the help of matrix.A() method, we are able to get
    1 min read
  • Python | Numpy matrix.std()
    With the help of matrix.std() method, we are able to find the standard deviation a matrix by using the same method. Syntax : matrix.std() Return : Return standard deviation of a matrix Example #1 : In this example we are able to find the standard deviation of a matrix by using matrix.std() method. #
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences