##### Slicing using vectors
 Slicing using vectors paul18fr Wafer-Thin Wafer Posts: 96 Threads: 23 Joined: Apr 2019 Reputation: 3 Nov-14-2019, 10:06 AM Hi all Does somebedoy know how to use vectors for slicing (see code herebellow)? I got the following error "only integer scalar arrays can be converted to a scalar index" but I do not understand since I'm using a scalar (numpy) array, or I'm missing something Thanks Paul ```n = 100 m = 2 A = np.random.randint(66, size=(n,m), dtype=np.int32) i = np.random.randint(n-4, size=int(0.5*n), dtype=np.int32) j = i + 4*np.ones(int(0.5*n), dtype=np.int32) extract1_A = A[i,:] # as usual = OK #extract2_A = A[i:i+4,:] # fails extract3_A = A[i:j,:] # fails``` Reply schuler Unladen Swallow Posts: 4 Threads: 1 Joined: Nov 2019 Reputation: 0 Nov-15-2019, 12:15 AM Try this: ```import numpy as np n = 100 m = 2 A = np.array(np.random.randint(66, size=(n,m), dtype=np.int32)) i = np.array(np.random.randint(n-4, size=int(0.5*n), dtype=np.int32)) j = i + 4*np.ones(int(0.5*n), dtype=np.int32) print(i) print(j) print(i.shape) print(j.shape) extract1_A = np.array(A[i,:]) extract2_A = np.array([ A[x:x+4,:] for x in i]) extract3_A = np.array([ A[x:y,:] for x in i for y in j]) print (extract1_A.shape) print (extract2_A.shape) print (extract3_A.shape)``` wish everyone happy coding Reply paul18fr Wafer-Thin Wafer Posts: 96 Threads: 23 Joined: Apr 2019 Reputation: 3 Nov-15-2019, 11:00 AM thanks for the interest, but the goal has ever been to avoid the use of loops. Be carefull with the dimensions of your matrixes Paul Reply micseydel Involuntary Spiderweb Collector Posts: 2,337 Threads: 60 Joined: Sep 2016 Reputation: 72 Nov-16-2019, 12:00 AM It's generally helpful if you post runnable code (yours lacks at least one import) and the full, verbatim error message (ideally in error tags). Here's what I get when I run your code after adding the import: ``````Error:Traceback (most recent call last): File "doit.py", line 10, in extract3_A = A[i:j,:] # fails TypeError: only integer scalar arrays can be converted to a scalar index`````` (Nov-14-2019, 10:06 AM)paul18fr Wrote: I got the following error "only integer scalar arrays can be converted to a scalar index" but I do not understand since I'm using a scalar (numpy) array, or I'm missing somethingSo I tried printing your object and I got something like this: ``````Output:[[22 45] [24 48] [51 24] [23 63] [ 9 29] .../``````That... looks like a collection of non-scalars to me. I don't usually link to SO, but this might be useful. Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more. Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :) Reply paul18fr Wafer-Thin Wafer Posts: 96 Threads: 23 Joined: Apr 2019 Reputation: 3 Nov-16-2019, 10:43 AM (Nov-16-2019, 12:00 AM)micseydel Wrote: It's generally helpful if you post runnable code (yours lacks at least one import) and the full, verbatim error message (ideally in error tags). Here's what I get when I run your code after adding the import: ``````Error:Traceback (most recent call last): File "doit.py", line 10, in extract3_A = A[i:j,:] # fails TypeError: only integer scalar arrays can be converted to a scalar index`````` The code has been added as it stands to highlight the issue I got. Finally I found a way that answers to my need without using any loop but the Kronecker product; it has been checked on a small size matrix, but it quite interesting with million of lines (tested with 10 million on my old laptop). Paul ```import time import numpy as np #n = 1_000_000 n = 10 m = 2 A = np.array(np.random.randint(66, size=(n,m), dtype=np.int32)) i = np.array(np.random.randint(n-4, size=int(0.5*n), dtype=np.int32)) j = i + 4*np.ones(int(0.5*n), dtype=np.int32) ## the i vector gives us the first index of values we want to get from A ## in the current case we want to get values from i to (i+4) ## with only 1 index, slicing is traditionnally used as A[100:104,4] for example ## the "trick" or the solution I've been using is to specify each index I want to extract ## using the Kronecker product as follow: t0 = time.time() k1 = np.arange(4, dtype=np.int32) k2 = np.ones(int(0.5*n), dtype=np.int32) k3 = np.ones(4, dtype=np.int32) kron1 = np.kron(k2,k1) # here [0 1 2 3] is repeated (0.5*n) times => from j vector kron2 = np.kron(i,k3) # here each index is repeated (0.5*n) times => from i vector index = kron1 + kron2 # then each index varies from its initial value to (initial+4) Extract_A = np.copy(A[index,:]) # all the indexes have been explicitly expressed and we can extract the values as usually t1 = time.time() print("The new solution took {} seconds".format(t1-t0))``` Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Scaling of mapped vectors? sricha1217 1 1,068 Apr-10-2018, 10:26 AM Last Post: sricha1217 Statistics: Two histograms based on word frequency vectors fancy_panther 2 2,501 Mar-27-2017, 01:18 AM Last Post: zivoni

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020