Arrays with ordinal set of values for index? - arrays

I am pulling my hair over this one.
I read in my data structures text, that the index of an array usually uses a consecutive range of integers, but the index can have any ordinal set of values.
I think I understand the consecutive range part, like 0,1,2...(in most languages) positions in an array where elements are placed, but what does an ordinal set of values mean?
EDIT
Here's a paragraph from the text:
"In computer programming, a group of homogeneous elements of a specific data type is know as an array. Arrays hold a series of data elements, usually of the same size and data type. The individual elements are accessed by their position in the array. This position is given by an index, also called the subscript. The index usually uses a consecutive range of integers, but the index can have any ordinal set of values."

It means, that usually array is indexed like [0, 1, 2, 3, ... k], but it may happen, that some particular array data-structure, can have a ordinal set of index values - index values can be like [1, 3, 5, 7.. k].

Related

Matching indices of arrays of different sizes

Let's say we have a DATA array consisting of 23 elements,
And INDICATORS array of 3 elements (might be whatever size you want: 3, 5, 7 etc but less then DATA array)
Let's say I'm iterating over DATA array, and starting from, say, index 4 in the DATA array, I need to start highlighting INDICATORS one by one, starting from the first, then second, then third and then wrapping back to the first.
For example:
DATA indexes:0...(4, 5, 6) (7, 8, 9)...22
INDICATORS indexes: (0, 1, 2) (0, 1, 2) ... etc
So basically I need to convert index 4 of DATA array to index 0 of the INDICATORS array, index 5 of DATA array to index 1 of the INDICATORS array etc.
dataArrayIndex % indicatorsArraySize doesn't work in this case.
How do I do that?
Thanks.
dataArrayIndex % indicatorsArraySize
Won't work for you because you have that starting index.
Instead, you have to subtract your starting index from the dataArrayIndex first:
(dataArrayIndex - dataStartIndex) % indicatorsArraySize
Alternatively, as you iterate, you can compare your current indicatorsArrayIndex to indicatorsArraySize, after incrementing indicatorsArrayIndex. If they're equal, reset indicatorsArrayIndex to 0.

Get specific cells from a cell array

I have a numeric array sized 1000x1 which have values 0 and 1, called conditionArray.
I have a cell array called netNames with the same size (1000x1) and its cells contain string values (which are name of some circuit nets).
I want to extract net names which from netNames which their pairwise condition bit is 1 in conditionArray.
E.g. if conditionArray(100) is equal to extract its net name from netNames{100}.
Output of this process can be stored in an string array or cell array.
Are there any ways to do this operation with pairwise operations or I should use a for statement for this?
You shall check out cellfun in Matlab anytime you want to manipulate each element inside a cellarray without using a for loop.
As I understand, you have:
N = 1000;
% an array with 0s and 1s (this generates random 0s and 1s):
conditionArray = randi([0,1],N);
% a cell array with strings (this generates random 5-character strings):
netNames = cell(N);
netNames = cellfun(#(c)char(randi([double('a'),double('z')],1,5)), netNames, 'UniformOutput',false);
To extract the elements from netNames where conditionArray is 1, you can do:
netNames(conditionArray==1)
This uses logical indexing into the cell array.

What is contiguous subarray

I am trying to study contiguous subarray but I am not getting any study material which explains this concept.
But I found one example with says that
Given the array [-2,1,-3,4,-1,2,1,-5,4] the contiguous subarray is [4,-1,2,1]
Can anyone explain on what bases they are saying contiguous subarray is [4,-1,2,1]
It isn't the contiguous subarray, there are many. It's just a subsequence without skipping any elements. E.g. [-2, 1], [-2, 1, -3], [2, 1, -5] are all contiguous subarrays of this array, but [2, 1, 4] isn't.
A contiguous subarray is simply a subarray of an array with a condition that the elements of the subarray should be in exact sequence as the sequence of the elements in the array.
for example if the array is [1,2,3,4,5]
then [1,3,5] is a subarray of the array, but not a contiguous subarray since the sequence does not match as the elements 2 and 4 are skipped.
[1,2,3] will be one of the contiguous subarrays.
Note: So I kept my original answer and extra thoughts here: https://cs.stackexchange.com/questions/43303/what-is-a-contiguous-subarray/149711?noredirect=1#comment313804_149711
The actual definition of contiguous subarray (as others have answered) is any sub series of elements in a given array that are contiguous ie their indices are continuous.
So given [1,2,3,4,5,6]:
[1,2,3], [3,4], [3,4,5,6] are all valid contiguous subarrays. Any algorithm can be used to generate the subarrays.
Personal note: What was confusing for me was most explanations either use or reference a specific problem or condition set to generate the subarrays. Most don't specifically state there's no direct relationship between the problem and the definition of contiguous subarrays.
For me it's the same as asking:
Q: "What does + do?"
A: "4+4=8"
Me: "OK, so I can only use + with 4, got it."
On my travels I'd see answers like this:
basically the sum of contiguous array must be greater, if you will try to add the all elements of array it will give you lowest sum, but if you will add this specific range in continuity you will get the greatest sum we can make out of this array.
Which is referring to some specific problem that uses contiguous subarrays, but no one was calling this out, so to a noob you could infer that contiguous subarrays has some further meaning.
This is how my brain worked to grok the definition, so hopefully it helps someone else out.
To get it fast:
Any subarray sliced out from a given array [0,...,n] keeping original continuous order of elements in it.
Which starts at element with chosen index between 0 and n-1
and ends at other chosen element of a higher index than the starting one.
Containing all elements of indexes in the chosen range.
[-2,1,-3,4,-1,2,1,-5,4] is indexed [0,1,2,3,4,5,6,7,8]
[4,-1,2,1] is a contiguous subarray being a range indexed: [3,...,6]
basically the sum of contiguous array must be greater, if you will try to add the all elements of array it will give you lowest sum, but if you will add this specific range in continuity you will get the greatest sum we can make out of this array.

Keeping track of indices after sorting

So I have several arrays of positions, velocities, etc in 3D-space (vec3(x,y,z)) and another array which holds indices that are used to look-up in the vec3 arrays. This is all for a particle-system representing cloth (in case anyone was wondering).
The second array is far larger than the position array because for each particle, the second array represents a type of "spring" relationship with another particle. So for example, any given particle might have a "spring" attached to 3 different particles. So the first bunch of indices in the second array might look like this: [0, 1, 0, 2, 0, 3, 0, 4, ...]
The problem here is that the position array is sorted based on a hash function used for finding neighbors in a uniform grid. This means that the indices held in the 2nd array will no longer be valid. I'm trying to figure out a way to sort the positions and then still use the 2nd array to index properly.
One thought that I've had would be to have a 3rd array which stores the new indices based on the sorting function, but I am not sure how I could actually go about doing this.
Also, the reason the data is separated rather than being put into an object is that this is being run in CUDA and it is an optimization for speed/memory.
Is there a simple way of going about this? Thanks for any help.
Would something like this work?
Transfer your array of vec3(x, y, z)s into an array of pair(index, vec3(x, y, z)). Sort this new array by taking the hash function of the second element. The result will be a sorted array of pair(index, vec3(x, y, z)), where index is the vector's initial position in the array. Then, use this to construct a third "lookup" array of integers whose indices are the initial indices and whose values are the new values. Now to get a vector from your second array you do something like vector_pairs[lookup[spring[4]]].second.
Python-ish pseudocode:
vecs = ...
spring = ...
pair_vecs = [(index, vec) for index, vec in enumerate(vecs)]
pair_vecs.sort(key=lambda index, vec: hash(vec))
lookup = [0] * len(pair_vecs)
for new_index, (initial_index, vec) in enumerate(pair_vecs):
lookup[initial_index] = new_index

Serialization of parallel integers

I would like to serialize parallel information like 1,23,5,32,6,1,0,5,1,6 into one single integer ("index" from now on).
The array has always exactly ten evalues and the order of the values matters. The values of the array are integers that can reach from 0 to +inf and are completely independent from each other.
I thought through a few possible solutions, but I can't come up with a solution where every possible array has exactly one possible index and where every possible index has exactly one possible array.
You can imagine the situation like ten ordered boxes in a row in which specific numbers of balls were put. Now there is an infinite number of these rows and each of the row has a different number of balls in the boxes. How can you give each of the rows one number (like a catalogue) and you exactly know how many balls are in which box?
I know that the index number is going to be ridiculously big with big array values, but I won't use big array values anyway.
examples:
Index: array
0: (0,0,0,0...,0,0)
1: (0,0,0,0...,0,1)
There should be a simple mathematical solution to this, but I don't see it.
You could just interleave the digits. Example with arrays of size 3 instead of 10:
(123, 456, 789) --> 147258369
insert leading zeros if necessary:
(123, 4, 5) --> 100200345
The above answer by henrik assumes that you know the maximum number of digits for a number, if you already know that then no need to interleave, just concatenate the numbers padded with zeros.
I have a general construct :) Assume only two dimensions and arrange the pairs like this and maintain two integers.
(0,0) \\offset:0 -- sum:0
(0, 1), (1, 0) \\offset:1+0 -- sum:1
(0, 2), (1, 1), (2, 0) \\ offset:1+2 -- sum:2
so essentially the index for a tuple is the index+offset where offset is determined by the level(fibbonacci(level), the level is determined by the sum of the terms, the index is determined by the ordering. (Offset+index) is guaranteed to be unique and can be decoded by dynamic programming
For more than two dimensions just recursively collapse dimensions, get an integer for first two dimensions this reduces your problem by 1 dimension and then you can keep going :)

Resources