Ok I want to pick a random point in the 2d array so it can be filled. Ive seen how to do this for a 1d array, and am wondering how this would be possible in a 2d array. All I have seen about this method is that the same position comes up again, which is a slight problem, but I don't know how to do it in the first place. The 2d array is essentially a grid, with the dimensions being the x and y coordinates. And the random element selecting a point within the boundaries (which is user selected but for the purposes of this problem can be 30x50.
EDIT:
import java.util.Random;
class pickRand{
public static String get (int x, int y){
int rndx = generator.nextInt(x) + 2;
int rndy = generator.nextInt(y) + 2;
}
}
So would this work, the x and y will correspond to the user generated number and have a raised boundary of 2 either side to prevent any objects going (partially outside or of the grid. Nothing needs to be returned right?
If you grid is of size M by N
Generate a random number between 0 and M-1 say i
Generate another random between 0 and N-1 say j
(i,j) will be a random element of the 2d array
What role does the array play here?
Essentially, the task is to pick... random integer 2D coordinates.
So if you want two coordinates, say i in 0...W-1 and j in 0...H-1, just draw two random integers. If you need more for higher dimensionality, draw more randoms.
Obviously, you can then access array[i][j].
In most languages, arrays can however be ragged, i.e. the rows/columns may have different lengths. This is however just as trivial to handle...
Related
I have a multidimensional array and while iterating through its every element, I need to calculate the volume of the square, cube, or other respective objects in case of dimensions higher than 3, in each element, the size of 2r. If I'm iterating through elements near the boundaries of the array, part of that square/cube is going to stick outside the array - and I need the volume of the intersection between the array and the object.
This is how the problem looks in 2D - I need to calculate the red area.
I know two ways so far of doing that:
Cases and if-statements. For 2D, I could calculate the coordinates of the corners of the intersection, but since this isn't strictly a 2D problem, but a multidimensional one, where the number of dimensions is given on input, cases and if-statements and subsequent hardcoding are out of question.
Manually iterating through every element in the square and checking if it belongs to the array. While this is easy to do, it is also incredibly slow, even in 2D, because I'm iterating through an n-dimensional array, and in each iteration, I'm again doing n loops the size of 2r^n. Bigger the radius, slower the execution.
Is there any way that would allow me to calculate the volume of those intersections fast?
If I understand your question correctly, you want to calculate the intersection volume between two axis-aligned hyperrectangles.
The first rectangle (your array) is defined by the position of its lower corner (arrayLower, an nD vector) and its size (arraySize, again an nD vector). The second rectangle is defined by its center (p, an nD vector) and an extent of r units in each direction.
For given dimensionality d, this can be done in a very structured way since you only have to multiply the extents in each dimension:
volume = 1
for each d:
lower = max(p[d] - r, arrayLower[d])
upper = min(p[d] + r, arrayLower[d] + arraySize[d])
if(lower > upper)
volume = 0 //no intersection
else
volume *= upper - lower
I have a matrix for M[100][100], all filled with ZERO. Now I want to fill randomly 100 grids by '1'. How do I do that?
Challenge I am facing:
When I randomly select any position (x, y), that position can be already filled by 1. I have to minimise rand() call count. Is it possible to fill within 100 rand() call limit?
The question was marked as duplicated with another question but that does not solve my problem? Any generalised idea for 2D matrix?
Use the Fisher-Yates solution from this answer. Specifically you'd want to once create an array that contains numbers 0 ... 9999, where x = i % 100 and y = i / 100, say.
Or if you want, you could as well use an array of struct coords { char x, y; }.
Then you shuffle only the first (or last) 100 elements, after which those first (last) 100 elements will be your coordinates. You can reuse the same array without subsequent initialization. Each 100 random fillings require only exactly 100 random numbers being generated.
I have a two dimensional grid where width and height are always the same.
[0][1][2]
[3][4][5]
[6][7][8]
I reduced it's data source into a one-dimensional array.
[0][1][2][3][4][5][6][7][8]
Access of elements works, but here comes the tricky part: How to know, whether a neighbour of a cell is outside the grid when still processing the one-dimensional array?
For example the upper right neighbour of [5] is out of the grid but using a calculcated offset index, I will get [3].
Anyone with experience in this field?
Well assuming you know (i) the indices of the cell and (2) the dimensions of the grid (sensible right?), and that its index in the 1D array is k, and in the 2D array i, j.
Then k = i * width + j. Thus i = k / width, j = k % width. (/ is integer division, % is modulus).
Once you get these two indices, you know the neighboring cell's indices, and you can check those against the boundaries as normal (I assume you know how).
I'm trying to create a cell array of size N,
where every cell is a randomized Matrix of size M,
I've tried using deal or simple assignments, but the end result is always N identical Matrices of size M
for example:
N=20;
M=10;
CellArray=cell(1,N);
CellArray(1:20)={rand(M)};
this yields identical matrices in each cell, iv'e tried writing the assignment like so:
CellArray{1:20}={rand(M)};
but this yields the following error:
The right hand side of this assignment has too few values to satisfy the left hand side.
the ends results should be a set of transition probability matrices to be used for a model i'm constructing,
there's a currently working version of the model, but it uses loops to create the matrices, and works rather slowly,
i'd be thankful for any help
If you don't want to use loops because you are interested in a low execution time, get rid of the cells.
RandomArray=rand(M,M,N)
You can access each slice, which is your intended MxM matrix, using RandomArray(:,:,index)
Use cellfun:
N = 20;
M = 10;
CellArray = cellfun(#(x) rand(M), cell(1,N), 'uni',0)
For every cell it newly calls rand(M) - unlike before, you were assigning the same rand(M) to every cell, which was just computed once.
Here's the stumper:
Start with three arrays A, B and C with a total of 2n+1 entries.
Write an algorithm to sort all of the entries from all of the arrays
using only the following two methods:
X = sort(X) replaces the array X with the sorted version.
(X , Y) = doubleUp(X , Y) does nothing if X has more elements
than Y, otherwise it removes the first length(X) entries from Y
and appends them to the end of X.
Here's what I've tried so far. If two of the arrays are empty, then just use sort on the nonempty array.
If one of the arrays is empty, then I think I can use doubleUp to get one array to have just one thing and the other array to have everything else, and if that singleton array has the smallest (or largest) element, then that works. So I can use sort after I use doubleUp each time to make sure this happens. I coded this up in Maple and it worked for all the cases I checked.
I have no idea how to do it with 3 arrays though. Anyone have any ideas?
Sounds like nonsense. The total number of entries is odd. The only way to increase the length of an array is to make it the smaller first argument of doubleUp, in which case it ends up with an even number of elements. So unless all the elements are in one array to begin with there's no way to make one array contain all the elements, sorted or otherwise.
So, the desired final result is not a single array containing all the elements in order. Or if it is, then the answer to the question is "it cannot be done".