Why do we use multiple dimensional arrays? - oop

I have an understanding about how multiple dimensional arrays work and how to use them except for one thing, In what situation would we need to use them and why?

Basically multi dimension arrays are used if you want to put arrays inside an array.
Say you got 10 students and each writes 3 tests. You can create an array like: arr_name[10][3]
So, calling arr_name[0][0] gives you the result of student 1 on lesson 1.
Calling arr_name[5][2] gives you the result of student 6 on test 3.
You can do this with a 30 position array, but the multi dimension is:
1) easier to understand
2) easier to debug.

Here are a couple examples of arrays in familiar situations.
You might imagine a 2 dimensional array is as a grid. So naturally it is useful when you're dealing with graphics. You might get a pixel from the screen by saying
pixel = screen[20][5] // get the pixel at the 20th row, 5th column
That could also be done with a 3 dimensional array to represent 3d space.
An array could act like a spreadsheet. Here the rows are customers, and the columns are name, email, and date of birth.
name = customers[0][0]
email = customers[0][1]
dateofbirth = customers[0][2]
Really there is a more fundamental pattern underlying this. Things have things have things... and so on. And in a sense you're right to wonder whether you need multidimensional arrays, because there are other ways to represent that same pattern. It's just there for convenience. You could alternatively
Have a single dimensional array and do some math to make it act multidimensional. If you indexed pixels one by one left to right top to bottom you would end up with a million or so elements. Divide by the width of the screen to get the row. The remainder is the column.
Use objects. Instead of using a multidimensional array in example 2 you could have a single dimensional array of Customer objects. Each Customer object would have the attributes name, email and dob.
So there's rarely one way to do something. Just choose the most clear way. With arrays you're accessing by number, with objects you're accessing by name.

Such solution comes as intuitive when you are faced with accessing a data element identified by a multidimensional vector. So if "which element" is defined by more than two "dimensions".

Good uses for 2D or Two D arrays might be:
Matrix Math i.e. rotation things in space on a plane and more.
Maps like game maps, top or side views for either actual graphics or descriptive data.
Spread Sheet like storage.
Multi Columns of display table data.
Kinds of Graphics work.
I know there could be much more, so maybe someone else can add to this list in their answers.

Related

Associating arbitrary properties with cells in Pandas

I’m fairly new to pandas. I want to know if there’s a way to associate my own arbitrary key/value pairs with a given cell.
For example, maybe row 5 of column ‘customerCount’ contains the int value 563. I want to use the pandas functions normally for computing sums, averages, etc. However, I also want to remember that this particular cell also has my own properties such as verified=True or flavor=‘salty’ or whatever.
One approach would be to make additional columns for those values. That would work if you just have a property or two on one column, but it would result in an explosion of columns if you have many properties, and you want to mark them on many columns.
I’ve spent a while researching it, and it looks like I could accomplish this by subclassing pd.Series. However, it looks like there would be a considerable amount of deep hacking involved. For example, if a column’s underlying data would normally be an ndarray of dtype ‘int’, I could internally store my own list of objects in my subclass, but hack getattribute() so that my_series.dtype yields ‘int’. The intent in doing that would be that pandas still treats the series as having dtype ‘int’, without knowing or caring what’s going on under the covers. However, it looks like I might have to override a bunch of things in my pd.Series subclass to make pandas behave normally.
Is there some easier way to accomplish this? I don’t necessarily need a detailed answer; I’m looking for a pointer in the right direction.
Thanks.

LeavePGroupsOut For multidimensional array

I am working on a research problem and due to a small sized dataset with subjects I am trying to implement Leave N Out style analyses.
Currently I am doing this ad-hoc and I stumbled upon scikit-learn LeavePGroupsOut function.
I read the docs but I am unable to understand how to use it in multidimensional array.
My data are the following: I have 50 subjects, around 20 entries per subject (not fixed) and 20 features per entry with ground-truth value (0 or 1) for every entry.
Well the documentation is actually pretty clear:
https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.LeavePGroupsOut.html#sklearn.model_selection.LeavePGroupsOut
In your case you need to concatenate your array s.t. you can provide for every entry and feature the group index. Thus your feature array will have the shape 50*20 datapoints times 20 features (1000,20), so your group array also needs to have shape (1000,).
Then you need to define the cross validation via
lpgo = LeavePGroupsOut(n_groups=n_groups)
It's important to notice that this will result in all possible combinations of left out test groups.

Getting the size of each dimension of multidimensional array

Let's say I want to write a function to "unpack" (store or log, perhaps) a multidimensional array using nested loops. The concept is simple enough, provided I'm able to determine, in the case of a 3D array, the length, width and height of the array.
In Objective-C, is there some way to, after being passed a multidimensional array of unknown size as a method argument, determine what those dimension sizes are? Then it'd be a simple matter of using, as stated, nested for loops.
NSArray is inherently one dimensional, a vector if you like, and as noted by John it has a count property. To simulate a multidimensional array you could of course have a NSArray *rows that contains a number of NSAarray *columnElement and so forth. This is not really a multi diensional array and more like a tree type structure but you could sort of call it an array. But I do not think that is what you are asking.
I think you are thinking of a C style buffer of memory traversed by pointer references. This is also inherently a one dimensional structure as memeory adressing is one dimensional. In many cases such a buffer can be viewed as 2 or more dimensional in such way that you say every 50 bytes is a row, so position 0 is row 1 col 1, position 49 is row 1 col 50 and position 50 is row 2 col 1 etc.
In this case as it is you as the designer who have defined this interpretation of the buffer as a two-dimensional array there cannot be a way to derive the structure from the buffer alone. Either you have to store the arrangement of the buffer as metadata in other variables or impose some form of delimeter characters in the buffer, e.g. newline for new row. and comma for new column element or similar (but then it is no longer an array in my opinion it is a file format, here a csv file).

How do I create a multidimensional array?

I need to keep 90x90 array data for iphone app. how can i keep this data? making an multi-dimensional array is a solution for this big table. or is there an other solution.
If the matrix is always 90x90, then you should just use C arrays.
Unless you have a special need for passing the matrix around, searching using predicates, or need some other feature of NSArray, then keep it simple.
You can:
Use a single Obj-C array containing 8100 elements and map your rows and columns onto the single index yourself: index = (row * 90) + column;
Create an Obj-C array containing 90 Obj-C arrays of 90 elements each.
Hash the row and column together into a single key that you can use with a dictionary. This could be a good solution especially if the array is sparse.
Use a single- or multi-dimensional C array, especially if the elements of the array are plain old C types, like int. If you're storing objects, it's better to go with an Obj-C container.
Iphone's have a built in database SQL-Lite. I'd look into that to see if it meets you needs

vb.net remove a row in two-dimensional array

In vb.net how do you delete a row in a two dimensional array?
If you need to remove items from an array you probably shouldn't use an array but should be using a list of some kind (List(Of List(Of String)) or something.
If you do want to stick with the array, there's two different solutions described on this page, one slow shift everything by hand and one faster that copies the memory. The samples are for one dimensional arrays but should be fairly easy to adapt.
If it is the last row you want to remove and you are using the second dimension to represent rows, you can use ReDim with the preserve option like so:
Dim myArray(2,1)
ReDim Preserve myArray(2, 2)
Warning: I suggestion you check out this article before using the above example: The Redim Preserve Performance Trap
If you need to remove a row in the middle, you are going to have to shift everything down a row first, then truncate the last dimension of the array.
This coupled with the need to pivot your concept of rows to the second dimension probably makes it more trouble than it is worth. Chances are, you are using the wrong type in the first place if you need to arbitrarily delete items like that. Traditional arrays (especially multi-dimensional ones) are really best used for fixed-size data sets.