I want to add two column of same matrix and append it
Input matrix:
[[74, 25],
[93, 76],
[77, 41]]
Required:
[[74, 99, 25],
[93, 169, 76],
[77, 118, 41]]
Note: 74 25= 99
93 76= 169
77 41= 118
Thanks in advance
CodePudding user response:
From your example is not clear the input war in numpy format. I guess yes due to the label numpy.
In this case, numpy cannot change shape so you must create a new data structure. These can be a good starting point
first
prepare the original matrix with all columns and fill it with the right values. Take care to do vectorial computation
second
data = array([[74, 25],
[93, 76],
[77, 41]])
A=data[:,0]
C=data[:,1]
B=A C
np.vstack([A,B,C]).T
# Out[67]:
# array([[ 74, 99, 25],
# [ 93, 169, 76],
# [ 77, 118, 41]])
CodePudding user response:
import numpy as np
a=np.array([[74, 25],
[93, 76],
[77, 41]])
b=np.ones((a.shape[0],a.shape[1] 1))
b[:,[0,2]]=a
b[:,1]=a.sum(axis=1)
print(b)