Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8849575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:41:16+00:00 2026-06-14T12:41:16+00:00

In NumPy: A = np.array([[1,2,3],[4,5,6]]) array([[1, 3, 5], [2, 4, 6]]) B = np.array([[1,2],[3,4],[5,6]])

  • 0

In NumPy:

A = np.array([[1,2,3],[4,5,6]])
array([[1, 3, 5],
       [2, 4, 6]])

B = np.array([[1,2],[3,4],[5,6]])
array([[1, 2],
       [3, 4],
       [5, 6]])

A.dot(B)
array([[35, 44],
       [44, 56]])

I only care about getting A.dot(B).diagonal() = array([35, 56])

Is there a way I can get array([35, 56]) without having to compute the inner products of all the rows and columns? I.e. the inner product of the ith row with ith column?

I ask because the performance difference becomes more significant for larger matrices.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:41:17+00:00Added an answer on June 14, 2026 at 12:41 pm

    This is just matrix multiplication for 2D arrays:

    C[i, j] = sum(A[i, ] * B[, j])
    

    So since you just want the diagonal elements, looks like you’re after

    sum(A[i, ] * B[, i]) # for each i
    

    So you could just use list comprehension:

    [np.dot(A[i,:], B[:, i]) for i in xrange(A.shape[0])]
    # [22, 64]
    

    OR, (and this only works because you want a diagonal so this assumes that if A’s dimensions are n x m, B’s dimensions will be m x n):

    np.sum(A * B.T, axis=1)
    # array([22, 64])
    

    (no fancy numpy tricks going on here, just playing around with the maths).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a 2D numpy array, I need to compute the dot product of every
I have a 2D numpy array. Is there a way to create a view
Given a numpy array and a __getitem__ -type index, is there an idiomatic way
import numpy a = numpy.array([20090913, 20101020, 20110125]) Can you explain why numpy.datetime64(a.astype(S8).tolist()) converts correctly
How can I create a huge numpy array using pytables. I tried this but
When I print a numpy array, I get a truncated representation, but I want
I've got a numpy array containing labels. I'd like to get calculate a number
Given an arbitrary numpy array ( ndarray ), is there a function or a
How can I build a numpy array out of a generator object? Let me
How do I get numpy array into python list? looking for ('foo', 1, 2,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.