1

I have a 2D array of (10,24) and a 1D array of (10,) shape. I want to slice a 2D array using 1D array such that my resultant array will be (10,24) but the values are sliced from indices in 1D array onwards.

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]]

I want my resultant array to be be:


[[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]]

2
  • 1
    Numpy does not support this natively as far as I know; it would require creating a jagged array as an intermediate state before padding out the zeros. Commented May 24, 2019 at 12:14
  • padding zeros is not really a requirement; numpy doesn't support variable shape 2D array that is why i thought pad it with zeros. Commented May 24, 2019 at 13:18

2 Answers 2

1

Here's a vectorized one with masking and also leveraging 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

Sample run -

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]])
Sign up to request clarification or add additional context in comments.

Comments

1

I don't think you can slice the array as you are padding with 0's. You can create an empty zeros array and populate it, for example

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

1 Comment

i wanted to solve this without loops. thanks anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.