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.
Related
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].
In the work I'm doing, I declare an array with some length N.
At some point during my code, the data between certain elements needs to be removed.
If my initial array is [1,2,3,4,5], I want to change it to [1,2,4,5]. But I don't just do it once. The process will involved removing each element until there are no elements left and I may take out more than one element at once. The reason why I do this is related a complicated process I'm trying to reproduce and I don't think its necessary to go into this right now.
What I want to be able to do is define an array
a = (/ 1, 2, 3, 4, 5 /)
and if I decided to remove elements 3 and 4 I could set up a pointer (call it p) such that it would point to a as:
p = (/ 1, 2, 5 /)
Specifically, p will have length that is equal to a minus the number of elements removed. I'd want to do this morn than once. Redefine a
a = (/ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 /)
First I remove elements 4 and 5 and point to it such that
p = (/ 1, 2, 3, 6, 7, 8, 9, 10 /)
And then later removed 7,8,9 I could point to a
p = (/ 1, 2, 3, 6, 10 /)
Meanwhile keeping a intact. The p above would in my head be created by pointing to the first three elements of a and then the 6th and then the 10th all at once in one compact pointer.
I know I can probably achieve the same results with allocatable arrays, but I see pointers as being extremely useful.
No. An array pointer can be associated any array section, but not arbitrary elements.
Depending on what you later want to do, you can have a second array that lists the current indices in a, and then use vector subscripting for certain operations. But there are limitations for where a vector subscripted array can be used (e.g. if associated with a dummy argument it cannot be redefined), and in some cases the underlying implementation of a vector subscripted array may simply be to create a temporary contiguous copy of the relevant elements anyway.
I usually approach it using an allocatable (data) array, and also another logical array that is a mask for the data array. Then use the mask to keep track of what elements of the array are 'in use' or 'not in use'. When the need arises the active or 'in use' elements can be packed into either a final or intermediate array and if it is intermediate then the logical array reset to the size of the new data array.
And I generally avoid pointers.
PACK https://gcc.gnu.org/onlinedocs/gfortran/PACK.html and SUM(logical array) may be useful here.
It depends on whether the access speed to an arbitrary position in the array is more important than the speed of erasing elements.
If erasing elements is your priority, you should definitely make a linked list structure in which you are able to erase n elements at once.
If, on the other hand, what you need is access speed to the data you should use an array.
I am new to Matlab and thus this could be a very trivial question and I appreciate those who take the time to help. I have a 618x2 matrix that has values in the first column and then the index of the value (circles on an image for this case). For example
46.9810, 1
0, 2
0, 3
0, 4
43.1429, 5
0, 6
0, 7...
This matrix is called 'Test2'
I have another matrix that is a 1x58 matrix (called overlapindex) The values in this matrix correspond to the index in the 'Test2' matrix
for example:
1, 3, 5, 7, 35, 37, 44, 49,....
I need a new matrix (let's call it NEW) that checks if the value in overlapindex has a nonzero correlating value in the 'Test2' matrix. For example, this NEW matrix would include [43.1429, 5] because the index is in both 'Test2' and also in 'overlapindex' and the corresponding value in 'Test2' is nonzero.
so essentially this 'NEW' matrix would look like...
46.9810, 1
43.1429, 5
and so on until all the indexes are checked and the 'NEW' matrix is made.
I just need to make sure that the index in 'overlapindex' corresponds to an actual non zero value in the 'Test2' matrix.
Please help and thank you in advance!
Your problem can be solved using logical indexing in Matlab:
NEW = Test2(overlapindex(Test2(overlapindex, 1) ~= 0), :)
Explanation
Test2(overlapindex, 1): rows which should be checked (accessing Nonconsecutive Elements)
Test2(overlapindex, 1) ~= 0: checks for every requested row if the condition is true
overlapindex(Test2(overlapindex, 1) ~= 0): indexes of the matched rows (logical indexing)
Note that this solution only works if overlapindex only contains existing indexes of Test2, but the solution could be easily extended to skip non-existing indexes.
I need to design an algorithm that finds the k'th smallest element in unsorted array using function that called "MED3":
This function finds the n/3 (floor) and 2n/3 (ceil) elements of the array if it was sorted (very similar to median, but instead of n/2 it returns those values).
I thought about kind of partition around those 2 values, and than to continue like QuickSelect, but the problem is that "MED3" doesn't return indices of the 2 values, only the values.
for example, if the array is: 1, 2, 10, 1, 7, 6, 3, 4, 4 it returns 2 (n/3 value) and 4 (2n/3 value).
I also thought to run over the array and to take all the values between 2 and 4 (for example, in the given array above) to new array and then use "MED3" again, but can be duplicates (if the array is 2, 2, 2, 2, ..., 2 I would take all the elements each time).
Any ideas? I must use "MED3".
* MED3 is like a black box, it runs in linear time.
Thank you.
I think you're on the right track, but instead of taking 2 to 4, I'd suggest removing the first n/3 values that are <= MED3.floor() and the first n/3 values that are >= MED3.ceil(). That avoids issues with too many duplicates. If two passes/cycle aren't too expensive, you can remove all values < MED3.floor() + up to a total of n/3 values = MED3.floor() (do the same for ceil())
then repeat until you are at the k'th smallest target.
I have an array of data. For simplicity, let's call it a 4 x 3 matrix. Let's say I want to find a data point in column 2 that has a value of 5. Then, I want to take all rows that contains the value of 5 in column 2 and place it in its own array. My data is much larger than the one displayed below, so I don't want to go through by eye and look at every line of data and identify all the 5's.
% My idea of the code:
data = [1 2 3 4; 5 5 5 6; 6 4 5 6]
if data(:,2) == 5
% This is the part I can't figure out
end
Let's call the finaldata the array in which the data with 5's will be stored. How do I do this?
You should use logical indexing:
all_fives_rows = data(data(:, 2) == 5, :)
You can use the FIND Function to search that value, and give the coords back (it might be a vector) to retrieve the rows:
data(find (data(:,2)==5),:)
Why not using logical indexing: Performance