- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Build a block matrix in Numpy

To build a block of matrix, use the **numpy.block()** method in Python Numpy. Blocks in the innermost lists are concatenated along the last dimension (-1), then these are concatenated along the secondlast dimension (-2), and so on until the outermost list is reached.

Blocks can be of any dimension, but will not be broadcasted using the normal rules. Instead, leading axes of size 1 are inserted, to make block.ndim the same for all blocks. This is primarily useful for working with scalars, and means that code like np.block([v, 1]) is valid, where v.ndim == 1.

## Steps

At first, import the required library −

import numpy as np

Creating two numpy arrays using the array() method. We have inserted elements of int type −

arr1 = np.eye(2) * 2 arr2 = np.eye(3) * 2

Display the arrays −

print("Array 1...

", arr1) print("

Array 2...

", arr2)

Get the type of the arrays −

print("

Our Array 1 type...

", arr1.dtype) print("

Our Array 2 type...

", arr2.dtype)

Get the dimensions of the Arrays −

print("

Our Array 1 Dimensions...

",arr1.ndim) print("

Our Array 2 Dimensions...

",arr2.ndim)

Get the shape of the Arrays −

print("

Our Array 1 Shape...

",arr1.shape) print("

Our Array 2 Shape...

",arr2.shape)

To build a block of matrix, use the numpy.block() method in Python Numpy −

print("

Result...

",np.block([[arr1,np.zeros((2, 3))], [np.ones((3, 2)), arr2]]))

## Example

import numpy as np # Creating two numpy arrays using the array() method # We have inserted elements of int type arr1 = np.eye(2) * 2 arr2 = np.eye(3) * 2 # Display the arrays print("Array 1...

", arr1) print("

Array 2...

", arr2) # Get the type of the arrays print("

Our Array 1 type...

", arr1.dtype) print("

Our Array 2 type...

", arr2.dtype) # Get the dimensions of the Arrays print("

Our Array 1 Dimensions...

",arr1.ndim) print("

Our Array 2 Dimensions...

",arr2.ndim) # Get the shape of the Arrays print("

Our Array 1 Shape...

",arr1.shape) print("

Our Array 2 Shape...

",arr2.shape) # To build a block of matrix, use the numpy.block() method in Python Numpy print("

Result...

",np.block([[arr1,np.zeros((2, 3))], [np.ones((3, 2)), arr2]]))

## Output

Array 1... [[2. 0.] [0. 2.]] Array 2... [[2. 0. 0.] [0. 2. 0.] [0. 0. 2.]] Our Array 1 type... float64 Our Array 2 type... float64 Our Array 1 Dimensions... 2 Our Array 2 Dimensions... 2 Our Array 1 Shape... (2, 2) Our Array 2 Shape... (3, 3) Result... [[2. 0. 0. 0. 0.] [0. 2. 0. 0. 0.] [1. 1. 2. 0. 0.] [1. 1. 0. 2. 0.] [1. 1. 0. 0. 2.]]

- Related Articles
- Build a block matrix from a list with depth one in Numpy
- Build a block matrix from a list with depth two in Numpy
- Matrix Block Sum in C++
- How to create a block diagonal matrix using a matrix in R?
- Generate a Vandermonde matrix in Numpy
- Interpret the input as a matrix in Numpy
- How to Flatten a Matrix using numpy in Python?
- Matrix product of two arrays in Numpy
- How to combine two matrices to create a block-diagonal matrix in R?
- Finding the number of rows and columns in a given matrix using Numpy
- How to create an identity matrix using Numpy?
- Matrix product of a 2D (first argument) and a 1D array (second argument) in Numpy
- Matrix product of a 1D (first argument) and a 2D array (second argument) in Numpy
- Generate a Vandermonde matrix and set the number of columns in the output in Numpy
- Can we have a try block without a catch block in Java?