Skip to content
Advertisement

Python Numpy make a pattern with a square matrix of any dimension

I am having difficulties trying to generate a specific pattern that would work for any square matrix with any square dimension using NumPy

For example:

User input: n = 3

Output:

JavaScript

User input: n = 5

Output:

JavaScript

User input: n = 8

Output:

JavaScript

Since a square matrix can be generated with any number in the form of (n x n), there would be instances where the user input is an odd number, how would I start figuring out the equations needed to make this work?


I got this going on but I was only able to do it on one corner of the matrix, any suggestion or idea is appreciated, thank you!

JavaScript

Input: n = 4

Output:

JavaScript

Advertisement

Answer

I looked around a bit more and was eventually able to pull it off, here’s my take on it.

JavaScript

Input: 3

Output:

JavaScript

Input: 5

Output:

JavaScript

Input: 8

Output:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement