Python Forum
How do I make a symmetric matrix from a column vector?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I make a symmetric matrix from a column vector?
#1
I have to transfer some code from MATLAB to python. The code takes a (300x1) column matrix, A, and converts it into a (24x24) symmetric matrix, B. As an example, A = [i1; i2; i3; i4; i5; i6; ...] and the lower traingle of B = [i1; i2 i3; i4 i5 i6; ...]. The MATLAB code is as follows:
n = 24;
Ind = zeros(n, n);
Ind((1:n) >= (1:n).') = 1:300;
Ind = Ind + Ind.' - diag(diag(Ind));
B = A(Ind);
When trying to implement this into Python I used the following format:
x = np.array(range(24))
y = np.array(range(300))
Ind = np.zeros(n)
Ind[x >= x.transpose()] = y
Ind = Ind + Ind.transpose() - np.diag(np.diag(Ind))
B = A[Ind]
But on the Ind[x >= x.transpose()] = y line I get the error "ValueError: NumPy boolean array indexing assignment cannot assign 300 input values to the 24 output values where the mask is true". Can someone please tell me where I'm going wrong in my translation or suggest a new approach to the problem to make the code more Python friendly? Thanks is advance.
Larz60+ write Mar-29-2021, 10:50 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Please use bbcode tags on future posts.
Reply
#2
Not a numpy expert here, but the following seems to work
import numpy as np

n = 5  # could be 24
N = n * (n + 1) // 2

x = np.array(range(n)).reshape((1, n))
ind = np.zeros((n, n))
ind[x <= x.transpose()] = np.array(range(N))
ind = ind + ind.transpose() - np.diag(np.diag(ind))

print(ind)
Output:
[[ 0. 1. 3. 6. 10.] [ 1. 2. 4. 7. 11.] [ 3. 4. 5. 8. 12.] [ 6. 7. 8. 9. 13.] [10. 11. 12. 13. 14.]]
Reply
#3
(Mar-29-2021, 10:33 AM)leocsmith Wrote: I have to transfer some code from MATLAB to python. The code takes a (300x1) column matrix, A, and converts it into a (24x24) symmetric matrix, B. As an example, A = [i1; i2; i3; i4; i5; i6; ...] and the lower traingle of B = [i1; i2 i3; i4 i5 i6; ...]. The MATLAB code is as follows:
n = 24;
Ind = zeros(n, n);
Ind((1:n) >= (1:n).') = 1:300;
Ind = Ind + Ind.' - diag(diag(Ind));
B = A(Ind);
When trying to implement this into Python I used the following format:
x = np.array(range(24))
y = np.array(range(300))
Ind = np.zeros(n)
Ind[x >= x.transpose()] = y
Ind = Ind + Ind.transpose() - np.diag(np.diag(Ind))
B = A[Ind]
But on the Ind[x >= x.transpose()] = y line I get the error "ValueError: NumPy boolean array indexing assignment cannot assign 300 input values to the 24 output values where the mask is true". Can someone please tell me where I'm going wrong in my translation or suggest a new approach to the problem to make the code more Python friendly? Thanks is advance.
Reply
#4
Thanks for this.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Check if two matrix are equal and of not add the matrix to the list quest 3 819 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  nsimplify, make zero the really small numbers in the matrix quest 5 1,628 Jan-19-2022, 11:41 PM
Last Post: quest
  Trying to search and make new column from the keyword. dgarg 1 1,479 Dec-20-2021, 08:41 PM
Last Post: deanhystad
Question How to understand the vector/direction mason321 0 1,105 Dec-14-2021, 10:57 PM
Last Post: mason321
  How to find vector of a 3D plot mason321 0 1,011 Nov-13-2021, 05:05 PM
Last Post: mason321
  How to replace column of a Matrix Joni_Engr 5 5,924 Aug-22-2021, 10:35 AM
Last Post: Joni_Engr
  How to multiply a matrix with herself, until the zero matrix results peanutbutterandjelly 3 3,352 May-03-2021, 06:30 AM
Last Post: Gribouillis
  3D vector class with inheritance from 2D vector class buss0140 4 3,112 Dec-20-2020, 08:44 PM
Last Post: deanhystad
  Issue with def norm in class Vector DimosG 4 2,478 Mar-26-2020, 05:03 PM
Last Post: DimosG
  Make dual vector dot-product more efficient technossomy 3 2,526 Nov-28-2019, 09:27 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020