Questions tagged [array-indexing]

Use this tag for questions about how to construct and interpret indices for retrieval of or assignment to non-trivial subsets or reorderings of multidimensional arrays.

Arrays are usually indexed numerically along their different dimensions. However, some packages, like numpy and MATLAB, allow for more complex objects to be used to compute the numerical indices. Questions with this tag should refer to such non-trivial indices. Examples include boolean masks, lists of integers (which behave differently in different languages), and linear indices into a multidimensional array.

75 questions
7
votes
1 answer

Do pointers support "array style indexing"?

(Self-answered Q&A - this matter keeps popping up) I assume that the reader is aware of how pointer arithmetic works. int arr[3] = {1,2,3}; int* ptr = arr; ... *(ptr + i) = value; Teachers/C books keep telling me I shouldn't use *(ptr + i) like in…
Lundin
  • 195,001
  • 40
  • 254
  • 396
5
votes
2 answers

How can I use enum values as index of array

I try to use enum value as index of array but it gives me an error. export class Color { static RED = 0; static BLUE = 1; static GREEN = 2; } let x = ['warning', 'info', 'success']; let anotherVariable = x[Color.RED]; <---- Error: Type…
sally
  • 379
  • 2
  • 8
  • 17
4
votes
1 answer

OrderedDictionary key name at specified index

Given an ordered dictionary, I would like to get the key at index 0. I can of course do a loop, get the key of the first iteration and immediately break out of the loop. But I wonder if there is a way to do this directly? My Google-Fu has not turned…
Gordon
  • 6,257
  • 6
  • 36
  • 89
4
votes
2 answers

How can numpy array indexing techniques can give different outputs for same input code?

#Code import numpy as np np.random.seed(124) x_2d = np.random.randint(1,50,(3,4,5)) print("I am getting wrong output => {}".format(x_2d[0][:][1])) print("This is what I want => {} ".format(x_2d[0,:,1])) # Code Ended # Output for above code I…
4
votes
1 answer

What is meant by ^1 as array index (e.g. arr[^1]) in C#?

int []arr = new int[4]; arr[^1]; // returns the last element I'm trying to figure out the syntax above. It's returning the last element, but why?
programmer
  • 95
  • 2
  • 7
4
votes
3 answers

Issue with pointers and postfixes

So I have to find out why specific values are printed out, and I've solved most of it but, I've got a problem with the last three. I'd be happy for any help int main(void) { int myValues[] = { 9, 0, 12345, 1, 7, 2, 6, 3, 5, 4 }; …
3
votes
1 answer

Are indexes easier to vectorize than pointers?

Is there any example (e.g. on https://godbolt.org/ ) where CLang generates worse code when an algorithm expressed by pointer iterations instead of array indexes? E.g. it can vectorize/unfold in one case but can't in the other one? In simple examples…
nponeccop
  • 13,527
  • 1
  • 44
  • 106
3
votes
1 answer

How do I pass in the name of an array index in perl?

Background I have these four lines of code that I am trying to extract into one function: my @firstList = split /\s+/, $doubleArray{A_I}{B_I}; @firstList = shuffle(@firstList); my @secondList = split /\s+/, $doubleArray{A_I}{C_I}; @secondList =…
isakbob
  • 1,439
  • 2
  • 17
  • 39
3
votes
2 answers

Using python range objects to index into numpy arrays

I've seen it once or twice before, but I can't seem to find any official docs on it: Using python range objects as indices in numpy. import numpy as np a = np.arange(9).reshape(3,3) a[range(3), range(2,-1,-1)] # array([2, 4, 6]) Let's trigger an…
Paul Panzer
  • 51,835
  • 3
  • 54
  • 99
3
votes
1 answer

Assembly: What is the purpose of movl data_items(,%edi,4), %eax in this program

This program (from Jonathan Bartlett's Programming From the Ground Up) cycles through all the numbers stored in memory with .long and puts the largest number in the EBX register for viewing when the program completes. .section .data data_items: …
3
votes
3 answers

How does a C program get information from an array internally?

I'm relatively new to programming, so when someone suggested that building an array of structs (each containing n attributes of a particular "item") was faster than building n arrays of attributes, I found that I didn't know enough about arrays to…
user2127595
  • 176
  • 14
2
votes
1 answer

2D array 3x9 matrix get the index of the next two elements of the current index and check if it is equal to 0

I am a total newbie to programming and i have been following some tutorials on array related to housie ticket generator.The point where I am stuck is that, I have to check each rows and each rows of the 3x9 matrix should not have more the two empty…
Akang Toshi
  • 193
  • 5
  • 17
2
votes
2 answers

Subdimensional array from multidimensional array in Julia

With NumPy I can access a subdimensional array from a multidimensional array without knowing the dimension of the original array: import numpy as np a = np.zeros((2, 3, 4)) # A 2-by-3-by-4 array of zeros a[0] # A 3-by-4 array of zeros but with…
Fredrik P
  • 682
  • 1
  • 8
  • 21
2
votes
2 answers

IOS/Swift: How to indicate the first position of the $0 in the .firstIndex ? 'Cannot convert value of type 'Any' to expected argument type 'String''

I would like to search a value in an array of arrays, like in this example but I don't know how to indicate it "the position of the first element in $0" ? var programArray: [Any] = [] let slots: String = "2021-14-09 08:00:00|2021-14-09…
ΩlostA
  • 2,501
  • 5
  • 27
  • 63
2
votes
1 answer

Is there a way to extract a solid box slice of an arbitrary multidimensional Python array given two sets of corner index coordinates?

Suppose I have a = np.arange(16).reshape(4,4), which is array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]]) And want to slice a as I could with a[0:3,1:4], which results in array([[ 1, 2, 3], …
1
2 3 4 5