Make A 2d Matrix Of Strings To Match Mesh Coordinates
Solution 1:
You can use numpy.array_split
or even numpy.split
to split the arrays into multiple sub-arrays. But the former does not raise an exception if an equal division cannot be made.
In [2]: np.array(np.array_split(df['Code'].values, 4))
Out[2]:
array([['aa1', 'aa2', 'aa3'],
['aa4', 'bb1', 'bb2'],
['bb3', 'bb4', 'ab1'],
['ab2', 'ab3', 'ab4']], dtype=object)
EDIT :
You mean like this?
In [5]: np.array(np.array_split(df.as_matrix(columns=['Code']), 4))
Out[5]:
array([[['aa1'],
['aa2'],
['aa3']],
[['aa4'],
['bb1'],
['bb2']],
[['bb3'],
['bb4'],
['ab1']],
[['ab2'],
['ab3'],
['ab4']]], dtype=object)
Solution 2:
Well, if you know the length of each row you can use just a list. You can make the 2D array in "your head" and convert the coordinates into their position in an array.
Example:
You have got a row_length x column_length grid, therefore you have a list with a row_length*column_length entries. To access a specific coordinate you access the following entry of the list:
Pos(x|y) = mygrid[xcoord*rowlength+columnlength]
Explanation/Clarification:
Instead of using an actual 2D array, you can just use a 1D list. If you know the size of each row, that isn't a problem but instead, increases the speed your program is working with. Let's assume we have got a 3x3 grid containing a letter at each position, our coordinates will look like that: (0|0, 0|1, 0|2, 1|0, 1|1, ...)
We could represent this grid with:
0 1 2
0 'a''q''x'
1 'm''f''b'
2 'l''s''r'
Or, instead of creating an actual 2D grid, we just create a 1D array.
data = ['a', 'q', 'x', 'm', 'f', 'b', 'l', 's', 'r']
To get the index of a specific coordinate we can now multiply the row_number with the length of each row and add the column_number to it.
For example, to access the coordinate (2|1) of the grid above, we can just access:
data[2*3+1]
If you check both values, you will see that both are delivering the letter 's' as it should.
Post a Comment for "Make A 2d Matrix Of Strings To Match Mesh Coordinates"