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.
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.