簡體   English   中英

使用索引1D數組切片2D數組

[英]Slicing a 2D array using indices 1D array

我有一個(10,24)的2D數組和(10,)形狀的一維數組。 我想使用1D數組對2D數組進行切片,以使我的結果數組為(10,24),但從1D數組中的索引開始對值進行切片。

import numpy as np
x1 = np.random.randint(1,20,10)

print(x1)
[ 8, 13, 13, 13, 14,  3, 14, 14, 11, 16]

y1 = np.random.randint(low = 1, high = 999, size = 240).reshape(10,24)

print(y1)

[[152 128 251 282 334 776 650 247 990 803 700 323 250 262 552 220 744  50
  684 695 600 293 138   5]
 [830 917 148 612 801 746 623 794 435 469 610 598  29 452 188 688 364  56
  246 991 554  33 716 712]
 [603  16 838  65 312 764 676 392 187 476 878 229 555 558  58 194 565 764
   48 579 447 202  81 300]
 [315 562 276 993 859 145  82 484 134  59 397 566 573 263 340 465 728 406
  767 408 294 115 394 941]
 [422 891 475 174 720 672 526  52 938 347 114 613 186 151 925 482 315 373
  856 155   5  60  65 746]
 [978 621 543 785 663  32 817 497 615 897 713 459 396 154 220 221 171 589
  571 587 248 668 413 553]
 [227 188   4 874 975 586  93 179 356 740 645 723 558 814  64 922 748 457
  249 688 799 239 708 516]
 [230 556 563  55 390 666 304 661 218 744 502 720 418 581 839 772 818 278
  190 997 553  71 897 909]
 [631 928 606 111 927 912  81  38 529 956 759   6 725 325 944 174  62 804
   82 358 305 291 454  34]
 [193 661 452  54 816 251 750 183  60 563 787 283 599 182 823 546 629 527
  667 614 615   3 790 124]]

我希望我的結果數組是:


[[990 803 700 323 250 262 552 220 744 50 684 695 600 293 138 5 0 0 0 0 0 0 0 0]
[452 188 688 364 56 246 991 554 33 716 712 0 0 0 0 0 0 0 0 0 0 0 0 0]
[558 58 194 565 764 48 579 447 202 81 300 0 0 0 0 0 0 0 0 0 0 0 0 0]
[263 340 465 728 406 767 408 294 115 394 941 0 0 0 0 0 0 0 0 0 0 0 0 0]
[925 482 315 373 856 155 5 60 65 746 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[785 663 32 817 497 615 897 713 459 396 154 220 221 171 589 571 587 248 668 413 553 0 0 0]
[64 922 748 457 249 688 799 239 708 516 0 0 0 0 0 0 0 0 0 0 0 0 0 0    ]
[839 772 818 278 190 997 553 71 897 909 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[6 725 325 944 174 62 804 82 358 305 291 454 34 0 0 0 0 0 0 0 0 0 0 0    ]
[546 629 527 667 614 615 3 790 124 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]

我不認為您可以在填充0時對數組進行切片。 您可以創建一個空的零數組並填充它,例如

y1_result = np.zeros(y1.shape)
for row, x_i in enumerate(x):
    for j, element in enumerate(y1[row, x_i:]):
        y1_result[row, j] = element

這是具有masking並利用broadcasting的矢量化功能-

def select_gt_indices(a, idx):
    r = np.arange(a.shape[1])
    select_mask = idx[:,None] <= r
    put_mask = (a.shape[1]-idx-1)[:,None] >= r
    # or put_mask = np.sort(select_mask,axis=1)[:,::-1]
    out = np.zeros_like(a)
    out[put_mask] = a[select_mask]
    return out

樣品運行-

In [92]: np.random.seed(0)
    ...: a = np.random.randint(0,999,(4,5))
    ...: idx = np.array([2,4,3,0])

In [93]: a
Out[93]: 
array([[684, 559, 629, 192, 835],
       [763, 707, 359,   9, 723],
       [277, 754, 804, 599,  70],
       [472, 600, 396, 314, 705]])

In [94]: idx
Out[94]: array([2, 4, 3, 0])

In [95]: select_gt_indices(a, idx)
Out[95]: 
array([[629, 192, 835,   0,   0],
       [723,   0,   0,   0,   0],
       [599,  70,   0,   0,   0],
       [472, 600, 396, 314, 705]])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM