使用新矩阵再次创建新矩阵?

时间:2018-05-22 14:03:20

标签: python numpy

我创建了一个代码,它从我的RangeInKM_Manufacturer数组中获取数字并减去矩阵中的每个元素,然后创建一个新矩阵,这很好用!

现在我想使用这个新矩阵并将其除以RangeInKM_By_Manufacturer然后乘以100以获得当前电池百分比状态,我该怎么做?

我的代码:

Electric_car = np.array({'Nissan Leaf 24 kWh pack', 'Nissan Leaf 30 kWh pack', 'Volkswagen e-Golf','Tesla Model S 90(D)','BMW i3 22kWh','BMW i3 33 kWh','KIA SOUL','Volkswagen e-Up(2016)','Renault ZOE R90 Z.E 40','Hyundai IONIQ','Mercedes-Benz B250E','Tesla model X 90D'});
RangeInKM_By_Manufacturer = np.array([199, 250, 300, 557, 190, 300, 250, 160, 403, 280, 200, 489])

City =     ['city1',       'city2',     'city3',      'city4',       'city5',  'city6',       'city7',      'city8' , 'city9', 'city10',   'city11','city12', 'city13',      'city14',        'city15',        'city16',    'city17', 'city18', 'city19',     'city20']
distances = [[0,             3.4,         4.5,           4,           5,       12.2,           5.4,          6.1,       5.7,     10.6,      12.8,    7.7,       7.4,           5,               12.4,             12.6,       8.7,      4.2,      3.8,         1.9],
            [3.4,            0,           2.2,           2.4,         4.3,     10.1,           2.8,          4.7,       4.5,     9.4,       11.6,    9.4,       9.4,           7.6,             14.6,             11.9,       8.7,      4.3,      4.5,         1.7],
            [4.5,            2.1,         0,             1.6,         4.5,     6.8,            1.6,          3.1,       3.8,     8.7,       11,      8.5,       9,             9.4,             17.2,             10.9,       8.1,      3.8,      4.8,         3.4],
            [4.1,            2,           1.6,           0,           2.7,     6.3,            1.6,          4.6,       5,       9.9,       12.1,    9.2,       9.4,           8.3,             15.9,             8.8,        9.3,      4.9,      5.2,          2.3],
            [5.1,            4.2,         4.5,           2.7,         0,       5.5,            3.5,          9.3,       7.5,     12.4,      14.7,    14.8,      12,            9.6,             15.8,             6.3,        11.8,     7.5,      7.8,          3.3],
            [12.2,          10.1,         6.8,            6.3,         5.5,     0,              5.7,         10.7,      12,      17,        19.2,    17,        17.2,          14.5,            21.9,             4.1,        16.3,     12,       14.3,         8.1],
            [5.4,            3.4,         1.6,           1.6,         3.5,     5.7,            0,            5.5,       5.8,     10.7,      12.9,    10.8,      10.2,          9.6,             17.2,             9,          10,       5.7,      6,           3.8],
            [6.1,            4.7,         3.1,           4.6,         9.3,     10.7,           5.5,          0,         4.1,     9.1,       11.3,    8.8,       10.9,          11.3,            20.8,             13.9,       8.4,      4.1,      6.4,          5.9],
            [5.7,            4.5,         3.8,           5,           7.5,     12,             5.8,          4.1,       0,       5.4,       7.7,     5.8,       9.4,           10.3,            17.2,             15.3,       4.8,      2.5,      5.3,          5.9],
            [10.6,           9.4,         8.7,           9.9,         12.4,    17,             10.7,         9.1,       5.4,       0,       2.6,     5.2,       10.2,          14.2,            20.5,             19.8,       4.1,      7.3,      9.1,          10.4],
            [ 12.8,          11.6,        11,            12.1,        14.7,    19.2,           12.9,         11.3,      7.7,     2.6,       0,       8.6,       13.6,          17.2,            23.6,             22.4,       6.7,      10.7,     11.9,         13],
            [ 7.7,           9.,          8.5,           9.2,         14.8,    17,             10.8,         8.8,       5.8,     5.2,       8.6,     0,         6.6,           9.9,             17.4,             19.8,       2.3,      5,        6,            9],
            [7.4,            9.4,         9,             9.4,         12,      17.2,           10.2,         10.9,      9.4,     10.2,      13.6,    6.6,       0,             5.1,             9.8,              17.8,       7.9,      6.6,      5.3,          9.5],
            [5,              7.6,         9.4,           8.3,         9.6,     14.5,           9.6,          11.3,      10.3,    14.2,      17.2,    9.9,       5.1,           0,               8.3,              13.9,       12.1,     8.2,      7.1,          5.7],
            [12.4,           14.6,        17.2,          15.9,        15.8,    21.9,           17.2,         20.8,      17.2,    20.5,      23.6,    17.4,      9.8,           8.3,             0,                22.5,       18.2,     14.4,     13.2,         13.1],
            [12.6,           11.9,        10.9,          8.8,         6.3,     4.1,            9,            13.9,      15.3,    19.8,      22.4,    19.8,      17.8,          13.9,            22.5,             0,          19.3,     15.1,     17,           9.6],
            [8.7,            8.7,         8.1,           9.3,         11.8,    16.3,           10,           8.4,       4.8,     4.1,       6.7,     2.3,       7.9,           12.1,            18.2,             19.3,       0,        6.3,      6.8,          9.9],
            [4.2,            4.3,         3.8,           4.9,         7.5,     12,             5.7,          4.1,       2.5,     7.3,       10.7,    5,         6.6,           8.2,             14.4,             15.1,       6.3,      0,        3.2,           5],
            [3.8,            4.5,         4.8,           5.2,         7.8,     14.3,           6,            6.4,       5.3,     9.1,       11.9,    6,         5.3,           7.1,             13.2,             17,         6.8,      3.2,      0,            4.8],
            [1.9,            1.7,         3.4,           2.3,         3.3,     8.1,            3.8,          5.9,       5.9,     10.4,      13,      9,         9.5,           5.7,             13.1,             9.6,        9.9,      5,        4.8,           0]]

 for EV_Start in RangeInKM_By_Manufacturer:
     for start_Driving in distances:
        Remaining_Range = [EV_Start-start_Driving]
     print(Remaining_Range)

我正在考虑创建一个名为“SoC”的新矩阵,在这里我有类似的东西:[(Remaining_Range/RangeInKM_By_Manufacturer) * 100]

我的想法是否正确?

1 个答案:

答案 0 :(得分:2)

如果我理解正确,你想把Remaining_Range和RangeInKM_By_Manufacture分开,我会从你离开的地方拿起。

您的Remaining_Range如下:

[array([ 487.1,  487.3,  485.6,  486.7,  485.7,  480.9,  485.2,  483.1,
         483.1,  478.6,  476. ,  480. ,  479.5,  483.3,  475.9,  479.4,
         479.1,  484. ,  484.2,  489. ])]

由于您尝试划分的两个数组都不相等,所以让我们先将它们设置为正确的格式,否则np.divide会抛出错误。

Remaining_Range = np.array(Remaining_Range)
Remaining_Range = Remaining_Range.flatten()
RangeInKM_By_Manufacturer = np.array(RangeInKM_By_Manufacturer)
RangeInKM_By_Manufacturer = np.vstack(RangeInKM_By_Manufacturer)

现在进行分组:

SoC = np.divide(Remaining_Range, RangeInKM_By_Manufacturer)

这将为Remaining_Range中的每个值提供值除以RangeInKM_By_Manufacturer。例如,487.1 / 199,487.3 / 199 ....... 489/199。然后循环跟随RangeInKM_By_Manufacture中的每个值。

我不确定您的百分比是什么意思,但如果您只是想将此结果乘以100,那么:

SoC = np.multiply(SoC, 100)

这是你期望的吗?