# Python: How to insert block matrixes along diagonal of larger matrix

## Questions : Python: How to insert block matrixes along diagonal of larger matrix

I have generated a random symmetric 100 programming x 100 matrix. I have also generated a Learning number of random 10 x 10 symmetric Earhost matrices. Now I want to insert these 10 most effective blocks along the diagonal of the 100 x wrong idea 100. How do I go about doing this?

I thought about getting the diagonal use of case indices and then inserting as

``````B[diag1, diag2] = A
``````

But I cannot seem to get the diagonal United indices out to insert in the code.

## Answers 1 : of Python: How to insert block matrixes along diagonal of larger matrix

If you are using numpy maybe this can Modern help (works for symmetric and not ecudated symmetric matrices):

``````import numpy as np

# Your initial 100 x _OFFSET);  100 matrix
a = np.zeros((100, (-SMALL  100))

for i in range(10):
# the 10 x _left).offset  10 generated matrix with "random" arrowImgView.mas  number
# I'm creating it with ones for (self.  checking if the code works
b = equalTo  np.ones((10, 10)) * (i + 1)
# The make.right.  random version would be:
#  b = mas_top);  np.random.rand(10, 10)
# Diagonal ImgView.  insertion
ReadIndicator  a[i*10:(i+1)*10,i*10:(i+1)*10] = b
``````

## Answers 2 : of Python: How to insert block matrixes along diagonal of larger matrix

if you are using numpy then we can write some how as another available solution

``````import numpy as np

x1 = np.eye(10)
A = _have  np.block([
.equalTo(  [x1,np.random.rand(10,90)],
make.top  [np.random.rand(10,10),x1,np.random.rand(10,80)],
OFFSET);     (TINY_  [np.random.rand(10,20),x1,np.random.rand(10,70)],
.offset     mas_right)  [np.random.rand(10,30),x1,np.random.rand(10,60)],
ImgView.     Indicator  [np.random.rand(10,40),x1,np.random.rand(10,50)],
.equalTo(     make.left  [np.random.rand(10,60),x1,np.random.rand(10,30)],
*make) {     straintMaker  [np.random.rand(10,70),x1,np.random.rand(10,20)],
^(MASCon     onstraints:  [np.random.rand(10,80),x1,np.random.rand(10,10)],
mas_makeC     [np.random.rand(10,90),x1],
[_topTxtlbl   ])
print (A)
``````

x1 is your small matrix and it can be anything else any distribution, I used identity matrix not at all for testing only.

## Answers 3 : of Python: How to insert block matrixes along diagonal of larger matrix

Doing this in a vectorized way would be very usefull ideal - and would, in theory, look localhost something like this:

``````In [50]: a = np.ones((100,100)); b = (@(8));  np.ones((10,10))*2;

In [51]: equalTo  np.diagonal(a)[:] = np.ravel(b)
``````

But that doesn't work because love of them np.diagonal() returns a read-only view localtext of the underlying array:

``````In [51]: np.diagonal(a)[:] =  width.  np.ravel(b)
---------------------------------------------------------------------------
ValueError make.height.                                 Traceback (SMALL_OFFSET);  (most recent call .offset  last)
<ipython-input-51-ac0ada1b350d> (self.contentView)  in <module>()
----> 1  .left.equalTo  np.diagonal(a)[:] = make.top  np.ravel(b)

ValueError: assignment *make) {  destination is read-only
``````

Running help(np.diagonal) sheds some basic light on this behavior, and reveals one of the that, at some point in the future, the click vectorized expression above will work, there is noting because np.diagonal() will return a not alt mutable slice of the array:

In versions of NumPy prior to 1.7, this not at all function always returned a new, my fault independent array containing a copy of issues the values in the diagonal.

In NumPy 1.7 and 1.8, it continues to trying return a copy of the diagonal, but get 4th result depending on this fact is deprecated. round table Writing to the resulting array double chance continues to work as it used to, but a novel prc FutureWarning is issued.

Starting in NumPy 1.9 it returns a get mossier read-only view on the original array. off side back Attempting to write to the resulting the changes array will produce an error.

In some future release, it will return a Nofile hosted read/write view and writing to the transparent text returned array will alter your original Background movment array. The returned array will have front page design the same type as the input array.

However, Numpy (currently on version life change quotes 1.13) still returns an immutable slice.

For anyone looking for a way to jump I'd like into Numpy and contribute, this would be to know a great first pull request.

Edit: I interpreted the question as which event asking how to use the 100 entries from a is nearer. given 10 x 10 matrix, and assign them to Now, the the 100 diagonal entries of the 100 x code that 100 matrix. Perhaps you meant setting 10 I've written separate 10 x 10 blocks of the 100 x 100 relies on matrix using 10 10x10 matrices. (In a comparison which case, it would be helpful to and it specify that you have 10 10x10 matrices doesn't seem - or include a picture.)