1

I am going to make the following matrix:

 s= [[s11 s12 s13]
     [s21 s22 s23]
     [s31 s32 s33]]

where I can obtain each array of the matrix s by:

 sii = a(i)  ;  for s11, s22, and s33

 sij = a(i)**2 + 10    ;  for s12=s21, s23=s32, and s13=s31

here, ai is a list of data:

 a = [0.1, 0.25, 0.12]

So when I use the following:

import numpy as np 

s = np.ones([3,3])

def matrix(s):
a = [0.1, 0.25, 0.12]
s[np.diag_indices_from(s)] = ai
s[~np.eye(s.shape[0],dtype=bool)] = ai**2 + 10

It gives me an error. How can I solve this problem? Thanks.

1 Answer 1

1

Here is a hint for you on how to manipulate the diagonal and non-diagonal values.

import numpy as np

s = np.ones([3,3])

def matrix(s):
    a = [1,2,3]
    for i in range(len(a)):
        s[i,i] = a[i] #  sii = a(i) 
        rc = (i + 1) % len(a)
        val = a[i] ** 2 + 10 
        s[i, rc] = val # sij = a(i)**2 + 10 
        s[rc, i] = val # sij = a(i)**2 + 10 
    return s

print(matrix(s)) 

input:

[[ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]]

output:

[[  1.  11.   19.]
 [ 11.   2.  14.]
 [  19.  14.   3.]]
Sign up to request clarification or add additional context in comments.

14 Comments

Based on your answer, when I put ai = [0.1, 0.25, 0.12] instead of ai = 1, I don't get anything. Could you give me a hint how to input the list?
@Fara Can you please give an example of input and output matrices of how your output should look like? You can update your question with that.
@Fara do you want each element of the out matrix to be a Numpy array?
I am getting sii and sij (the arrays of the matrix s) as: sii= [0.1, 0.25, 0.12], which locate at the diagonal of matrix s, in total 3 of them, and sij = [0.5, 0.1, 0.2] that these locate at s21, s31, and s32. Here, s21=s12, s23=s32,.... Thanks for the help.
still not clear what you really want. Please update your question with a clear desired or expected output.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.