Fast access to a MATLAB array of structs based on its id - arrays

I have a Matlab struct:
a(1).x=54.23; a(1).y=2.3; a(1).col=32.221; a(1).id=1;
a(2).x=5.23; a(2).y=3.3; a(2).col=2.221; a(2).id=2;
... and so on. Now i want to access the struct in a having id 73. I can think of doing a for loop but the thing is i have to access elements of array a several times like this based on id. Wat is the fastest data structure available for this purpose? Python like dictionary may work but i am not sure ow to implement it. Pointing out some code examples would be very helpful.

Try this:
id=[a.id];
a(id==73)
It's not as efficient as a dictionary, but if it's fast enough for your purposes it's not worth looking further.
The a.id part evaluates to a comma-separated list of id values, which are concatenated in an array that you can then use for lookup.

Related

SPARQL: array-strings conversion / extraction

I am doing a SPARQL Query that for one variable gives me the output "[-1.6101874126499998e-19]". This is obviously a string containing an array (that could also contain more numbers). Is there a way to access the number in it or does it need to be done in the underlying graph?
If it needs to be changed in the graph, what is a good ontological way to create multi-dimensional arrays...?
xsd:float(SUBSTR(?var,2,STRLEN(?var)-2)) does the job in this case, but it's quite hacky :/

Sorting Mechanism Reversing with Delphi

i knew its a weird Idea to think about, i want to know if it is possible to reverse the mechanism of sorting, (i dont want to reverse the order).
for example lets say i have a random array of integers, then i sorted the array with quicksort method, now i want to go back and un-sort the array and get it back to the it was.
you may suggest i save a copy of the array, thats not what i want, think of it as a time line and have the ability to go backward or forward of sorting method.
and if it is possible please consider showing me the best way to do it with Delphi XE.
Thanks in advance.
It is not possible to unsort. You have to either:
create a separate array that holds a copy of the values and then sort that array so that you can preserve the original array.
create a separate array that holds pointers/indexes to the values in the original array, and then sort the second array using the values it refers to.
I usually use a function that given an array returns an array of the sorted indexes.
This way you will always have the original data and you will be able to access the data in a sorted way using something like:
for jIndex in ASortedIndexesArray do
ShowMessage(AOriginalArray[jIndex]);
Hope this helps.
Mirko
If you want to think of it as a time line and have the ability to go backward or forward of sorting method then organize it like a timeline - with records in a file. Save each step and you will be able to reproduce it.
If the array is of integers the indices will not help you as one index (a pointer) takes the same memory as one array element. If you lack RAM use a file to store and retrieve the array. If you use larger data structures you can create, store and retrieve indices as David suggested.
You could log the swapping actions that quicksort (or whatever sorting algorithm you use) does to a list and then go forward and backward in that list to undo/redo these actions. Not simple to implement, but doable.

getting around octave's lack of map data structure

i am using octave for my project as i cant afford a matlab license, however i have run into a significant roadblock and that is the lack of associative array data structures.
my problem is this:
i have some data in the form of cell arrays containing matrices with each cell array representing a potential solution to a problem. i also have a floating point number that represents the evaluated performance of that solution which i want to put into a map-like data structure with the floating point score as the key, in order to sort the solutions by their performance.
can anyone suggest a simple solution to this problem?
what i have thought about doing so far is making each element part of a two element cell array, with the evaluation score as the first element and the data as the second, and then put those arrays into another cell array, which i then apply some sorting algorithm to, sorting by array{i}{1}.. but this seems like a pretty clunky solution.
does octave have any functionality in this respect that i am just unaware of? or is my clunky solution the only way to achieve this?
any help would be greatly appreciated
thanks
How about keeping the cell array as it is, but create a matrix where the columns are the evaluation score and the index into the cell array. Then you can easily use sortrows on the evaluation score column and use the index to pull the solution from the cell array. I think this should be a simple solution that has the benefit of not rearranging your potentially large set of data.

Searching in only one subarray of 3d array in Processing

I have a 3D array of int values and I want to search just through one of the subarrays for a specific value. While I could for-loop my way through every possible combination of the below code.
array[numberIwant][1-255[1-255];
That seems like overkill. I've come across the foreach type of for and thought that might be the answer to my quest but either it's not or I don't understand it well enough to get it to work. Could anyone suggest the way this should be done?
The foreach and the for loop will have almost identical processing time for an array of that size.
Although it might seem like overkill it is not, you will need to do a triple nested for loop then have an if statement seeing if the number you want was found.

Why use arrays in VBA when there are collections?

many people use extensively arrays in Excel/VBA to store a list of data. However, there is the collection object which in my view is MUCH MUCH more convenient (mainly: don't need to re/define length of the list).
So, I am sincerely asking myself if I am missing something? Why do other people still use arrays to store a list of data? Is it simply a hangover of the past?
Several reasons to use arrays instead of collections (or dictionaries):
you can transfer easily array to range (and vice-versa) with Range("A1:B12") = MyArray
collections can store only unique keys whereas arrays can store any value
collections have to store a couple (key, value) whereas you can store whatever in an array
See Chip Pearson's article about arrays for a better understanding
A better question would rather be why people would use collections over dictionaries (ok, collections are standard VBA whereas you have to import dictionaries)
#CharlesWilliams answer is correct: looping through all the values of an array is faster than iterating a Collection or dictionary: so much so, that I always use the Keys() or Items() method of a dictionary when I need to do that - both methods return a vector array.
A note: I use the Dictionary class far more than I use collections, the Exists() method is just too useful.
There are, or course, drawbacks to collections and dictionaries. One of them is that arrays can be 2- or even 3-Dimensional - a much better data structure for tabulated data. You can store arrays as members of a collection, but there's some downsides to that: one of them is that you might not be getting a reference to the item - unless you use arrItem = MyDictionary(strKey) you will almost certainly get a 'ByVal' copy of the array; that's bad if your data is dynamic, and subject to change by multiple processes. It's also slow: lots of allocation and deallocation.
Worst of all, I don't quite trust VBA to deallocate the memory if I have a collection or dictionary with arrays (or objects!) as members: not on out-of-scope, not by Set objCollection = Nothing, not even by objDictionary.RemoveAll - it's difficult to prove that the problem exists with the limited testing toolkit available in the VBE, but I've seen enough memory leaks in applications that used arrays in dictionaries to know that you need to be cautious. That being said, I never use an array without an Erase command somewhere.
#JMax has explained the other big plus for arrays: you can populate an array in a single 'hit' to the worksheet, and write back your work in a single 'hit.
You can, of course, get the best of both worlds by constructing an Indexed Array class: a 2-dimensional array with associated collection or dictionary objects storing some kind of row identifier as the keys, and the row ordinals as the data items.
Collections that auto-resize are slower (theoretically speaking, different implementations will obviously have their own mileage). If you know you have a set number of entries and you only need to access them in a linear fashion then a traditional array is the correct approach.

Resources