-1

I have following code:

a = np.arange(25).reshape(5,5)
b = np.arange(25).reshape(5,5)
c = a.astype( str )
d = b.astype( str )

Matrix a and b are:

array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])

Matrix c and d are:

array([['0', '1', '2', '3', '4'],
       ['5', '6', '7', '8', '9'],
       ['10', '11', '12', '13', '14'],
       ['15', '16', '17', '18', '19'],
       ['20', '21', '22', '23', '24']], dtype='|S11')

I want to get the following matrix by using c and d manipulation.

array([['00', '11', '22', '33', '44'],
       ['55', '66', '77', '88', '99'],
       ['1010', '1110', '1212', '1313', '1414'],
       ['1515', '1616', '1717', '1818', '1919'],
       ['2020', '2121', '2222', '2323', '2424']], dtype='|S11')

How to do?

Baikal
  • 33
  • 1
  • 4
  • I was in the process of suggesting `c.astype(object)+ d.astype(object)` when @Divakar so rudely closed this question. :) A little more on why object dtype works, see my answer earlier this month: https://stackoverflow.com/a/52675865/901925 – hpaulj Oct 17 '18 at 07:06

1 Answers1

1

Why not do

>>> np.core.defchararray.add(a.astype(str), b.astype(str))
array([['00', '11', '22', '33', '44'],
       ['55', '66', '77', '88', '99'],
       ['1010', '1111', '1212', '1313', '1414'],
       ['1515', '1616', '1717', '1818', '1919'],
       ['2020', '2121', '2222', '2323', '2424']], dtype='<U22')


Reproducibility material
import numpy as np
a = np.array(
    [[ 0,  1,  2,  3,  4],
     [ 5,  6,  7,  8,  9],
     [10, 11, 12, 13, 14],
     [15, 16, 17, 18, 19],
     [20, 21, 22, 23, 24]]
)
b = np.array(
    [['0', '1', '2', '3', '4'],
     ['5', '6', '7', '8', '9'],
     ['10', '11', '12', '13', '14'],
     ['15', '16', '17', '18', '19'],
     ['20', '21', '22', '23', '24']],
    dtype='|S11'
)
keepAlive
  • 6,369
  • 5
  • 24
  • 39