Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create a 2D array where all elements have the same number?

I would like to create an array size 576×720 where each element has the same number.

I tried it like this but did not succeed:

#Define row and column
int row = 576, col = 720;
#Initialize 2D character array
char array2D[row][col] = {[0 ... (row-1)][0 ... (col-1)] = '137'};

How can I create such an array?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Personally speaking, the easiest way would be using numpy module and full function:

row = 576
col = 720
valueToFill = 137
numpy.full((row, col), valueToFill)

Output

array([[137, 137, 137, ..., 137, 137, 137],
       [137, 137, 137, ..., 137, 137, 137],
       [137, 137, 137, ..., 137, 137, 137],
       ...,
       [137, 137, 137, ..., 137, 137, 137],
       [137, 137, 137, ..., 137, 137, 137],
       [137, 137, 137, ..., 137, 137, 137]])

If you wanna make sure the shape of the matrix is as same as what you asked for, you can try calling shape attribute on the array:

row = 576
col = 720
valueToFill = 1
array = numpy.full((row, col), valueToFill)
array.shape

which results in:

(576, 720)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading