I want to load some matrix into my program and then I want to divide it into smaller blocks.
What I want exactly can be seen on an image below:
http://postimg.org/image/aki19hjx9/ba463111/
In red squares are 3 examples of my "blocks" in which I would like to divide whole matrix. In this case each block should be (smaller) 3x3 matrix. I know how to load it into 2d array, but what should I do then?
int[][] bigMatrix = new int[9][9];
// initialize bigMatrix
int[][][] smallMatrices = new int[3][3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
smallMatrices[i][j][k] = bigMatrix[3*i+j][3*i+k];
}
}
}
// The submatrices are now in smallMatrices[i], 0 <= i < 3
Related
I'm having trouble filling a matrix with values I get by iterating through a method. I want to use a 3x3 matrix and then fill it with the values I obtain by iterating my method from 0 to 8. My idea was a for-loop but it does not work unfortunately. I would be glad if someone could help or has a link where I can look that up.
int[][] matrix = new int[3][3];
for (int i = 0; i < matrix.length; i++){
for (int j = 0; j < matrix[i].length; j++){
for(int a = 0; a < 9; a++) {
matrix[i][j] = fields.get(a).getSign().getFieldValue();
}
}
}
Correct me if I'm wrong.
The way I understood your question was you want to fill the matrix like this:
012
345
678
In that case you can do the first 2 forloops and add some maths, to get the correct numbers on every position:
int[][] matrix = new int[3][3];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = i * matrix[i].length + j;
}
}
The way this works is for every row (i) you multiply the rownumber by the rows length (the number of columns) and add the current column to it
To iterate through a two dimensional field (e.g. a matrix), you can use two for loops:
int dimension = 3;
int [][] matrix = new int [dimension][dimension];
for (int i = 0; i < dimension; i++){
for ( int j = 0; j < dimension; j++){
matrix[i][j] = fields.get(i).get(j);
}
}
I don't exactly know how you want to retrieve the values, but your current call looks suspicious to say the least :-)
It will simply assign the same value to all ints in column j.
If you simulate your loop, this is what it looks like
i -> 0
j -> 0
matrix[0][0] -> loop over all values from field
this way you would be putting the fields.get(8) into each index in your matrix.
#Alan's answers show how to properly loop and fill a 2d matrix from a 1d matrix
I'm trying to write a method that multiplies two matrices together and returns a 2D array with the resulting product. For example, the first matrix is a 5 x 2, and the second is a 2 x 5. The resulting matrix should be a 5 x 5 matrix respectively.
This is what I have so far:
public static int[][] multiplyArrays(int[][] arrayA, int[][] arrayB) {
int[][] arrayProduct = new int[arrayA.length][arrayB[0].length];
for (int cRow = 0; cRow < arrayA.length; cRow++) {
for (int dCol = 0; dCol < arrayB[0].length; dCol++) {
for (int i = 0; i < arrayB[0].length; i++) {
arrayProduct[cRow][dCol] += arrayA[cRow][i] * arrayB[i][dCol];
}
}
}
}
Whenever I'm runnning this code, it keeps giving me an exception saying ArrayIndexOutOfBoundsException: 2?
What is wrong with my logic in the code?
Any help is greatly appreciated!
EDIT: The fixed version of this code would be
public static int[][] multiplyArrays(int[][] arrayA, int[][] arrayB) {
int[][] arrayProduct = new int[arrayA.length][arrayB[0].length];
for (int cRow = 0; cRow < arrayA.length; cRow++) {
for (int dCol = 0; dCol < arrayB[0].length; dCol++) {
for (int i = 0; i < arrayA[0].length; i++) {**
arrayProduct[cRow][dCol] += arrayA[cRow][i] * arrayB[i][dCol];
}
}
}
}
In the inner-most loop use
for (int i = 0; i < arrayA[0].length; i++)
instead of
for (int i = 0; i < arrayB[0].length; i++)
I have this matrix :
So 2 line and 2 columns.
1 2
3 4
I have the reading function
for (int i = 0; i < m; i++) {
for (int j = 1; j < n; j++) {
try {// System.out.println("number is ");
a[i][j] = scanner.nextInt();
} catch (java.util.NoSuchElementException e) {
// e.printStackTrace();
}
}
} //print the input matrix
How can I make a border to a matrix? I've seen that in java there is no index -1.
I want a border with a number. For example :
0 0 0 0
0 0 0 0
0 1 2 0
0 3 4 0
0 0 0 0
How should I make that border function?
So, you need a (m+2) x (n+2) matrix:
// initialize m and n
...
// initialize the matrix with 0s
int a[][] = new int[m+2][n+2];
Then ignore the first elements (i and j should skip 0) and the last elements (i should skip m+1, j should skip n+1):
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
try {
a[i][j] = scanner.nextInt();
} catch (java.util.NoSuchElementException e) {
// e.printStackTrace();
}
}
}
First you need to create the matrix with extra rows and columns, for example in your case a 4x4 matrix. and them put 0 on the borders
when i==0 or i==n-1, j==0 or j==n-1,
int a[][] = new int[4][4];
int n,m;
n=4;
m=4;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(i==0 || j==0 || i==m-1 || j==n-1){
//a[i][j] = 0;
a[i][j] = 1;
}
System.out.print(a[i][j]+" ");
}
System.out.println();
}
then
for (int i = 1; i < m-1; i++) {
for (int j = 1; j < n-1; j++) {
try {// System.out.println("number is ");
a[i][j] = scanner.nextInt();
} catch (java.util.NoSuchElementException e) {
// e.printStackTrace();
}
}
} //print the input matrix
Elaborating on my comment:
If you first put in words what creating a border means, then translating it from English to Java is a simple task.
Let's take a look at the the two matrices that you have given:
Original one:
1 2
3 4
With border:
0 0 0 0
0 1 2 0
0 3 4 0
0 0 0 0
We see that when creating a border, we increase the width and height of the matrix by 2 respectively (to have an empty row of 0s on the top-bottom, and left-right), so instead of a 2x2 matrix we now have 4x4, and the indices of the elements are incremented by 1 (every element is pushed one step to the right, and one step downwards).
Putting this is in code:
int[][] createBorder(int[][] matrix) {
//this is our 4x4 matrix
int[][] borderedMatrix = new int[matrix.length+2][matrix[0].length+2];
//fill the 4x4 matrix with 0's
for(int i = 0; i < borderedMatrix.length; i++) {
for(int j = 0; j < borderedMatrix[0].length; j++) {
borderedMatrix[i][j] = 0;
}
}
//copy the values of the 2x2 into the 4x4 matrix, but push them one step to the right, and one step downwards
for(int k = 0; k < matrix.length; k++) {
for(int l = 0; l < matrix[0].length; l++) {
borderedMatrix[k+1][l+1] = matrix[k][k];
}
}
return borderedMatrix;
}
The simplest way would be to create a matrix of n+1xm+1, populate the border with 0 then fill in the remainder with the original nxm matrix.
So if I had your example matrix
int[][] original = {{1, 2},
{3, 4}};
int borderWidth = original[0].length + 2;
int borderHeight = original.length + 2;
int borderArray = new int[borderHeight][borderWidth];
for (int i = 0; i < borderWidth; i++) { //border
borderArray[0][i] = 0; //populate top row
borderArray[borderHeight - 1][i] = 0; //populate bottom row
if (i == 0 || i == borderWidth - 1) { //populate left and right columns
for (j = 1; j < borderHeight - 1; j++) {
borderArray[j][i] = 0;
}
}
}
for (int i = 0; i < original.length; i++) { //populate middle with original
System.arraycopy(original[i], 0, borderArray[i+1], 1, original[].length);
}
This question is for Java. I keep on trying to create a Block[][] of size N, where the numbers 0 - 9 are placed randomly. I've created an array and then shuffled it to prevent numbers from being repeated. When I iterate over the array and assign the numbers to the two dimensional array, I keep on finding that all of the numbers in the two dimensional array are the same. The block keeps on coming out as:
222
222
222
I'd greatly appreciate your help!
public class Board {
int N = 3;
static int [][] copy;
//construct a board from an N-by-N array of blocks
//(where blocks[i][j] = block in row i, column j)
public Board(int[][] blocks){
blocks = new int[N][N]; //creates array of size N
//generates random numbers 0 inclusive to # exclusive
List<Integer> randomList = new ArrayList<>();
for (int i = 0; i < blocks.length; i++){
randomList.add(i);
}
int randomNum = 0;
for (int i = 0; i < blocks.length; i++){
randomNum = randomList.get(i);
}
Collections.shuffle(randomList);
for (int i = 0; i < blocks.length; i++){
for (int j = 0; j < blocks[i].length; j++){
blocks[i][j] = randomNum;
}
}
copy = blocks.clone();
}
int counter = 0;
Collections.shuffle(randomList);
for (int i = 0; i < blocks.length; i++){
for (int j = 0; j < blocks[i].length; j++){
blocks[i][j] = randomList.get(counter);
counter++;
}
}
You should create a counter that will loop through each element of your original arraylist and assign to the multi-dimensional arraylist
Also change:
for (int i = 0; i < 10; i++){
randomList.add(i);
}
You say you want 0-9 the above loop will give you 0-9. The loop you have will only give you 0-2.
The problem is you're assigning the value from randomNum to all of your cells. This variable is defined before your second for-loop and is left at the highest value at which that loop terminated (in your example the value 2).
You probably want to add a value from your randomList and then reshuffle it when the i index changes.
Your code for selecting a random number has no randomness in it at all:
int randomNum = 0;
for (int i = 0; i < blocks.length; i++){
randomNum = randomList.get(i);
}
You do shuffle randomList afterwards, but then make no use of the shuffled list.
You need to use the value in your random list of the position you are setting. In addition to this, if you want 0-9 values to be inserted, you need to modify the random list creation:
public Board(int[][] blocks){
blocks = new int[N][N]; //creates array of size N
//generates random numbers 0 inclusive to # exclusive
List<Integer> randomList = new ArrayList<>();
for (int i = 0; i < 10; i++){
randomList.add(i);
}
Collections.shuffle(randomList);
for (int i = 0; i < blocks.length; i++){
for (int j = 0; j < blocks[i].length; j++){
blocks[i][j] = randomList.get(j);
}
// Shuffle the list again to get different values for the next line
Collections.shuffle(randomList);
}
copy = blocks.clone();
}
I need to try and duplicate and image, created by array(a), both vertically and horizontally to make a square full of the repeating image. So starting with 1 square image and I want to duplicate it 2 times horizontally and 2 times vertically it will create another image with 4 of the initial image, 2x2.
public static int[][] replicate(int[][] a) {
int[][] replicated = new int[a.length * 2][a[0].length * 2];
for (int r = 0; r < 2; r++) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
replicated[i][j + r * a[i].length] = a[i][j];
}
}
}
for (int r = 0; r < 2; r++) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
replicated[i + r * a.length][j] = a[i][j];
}
}
}
return replicated;
}
This gets them across the horizontal for 1 line, and vertical for 1 line but does not fully fill in. So if I ran this I would be missing the bottom right of the 4 images. I'm having trouble putting them together.
public static int[][] replicate(int[][] image) {
int[][] replicated = new int[a.length * 2][];
for (int y=0; y<image.length; y++) {
int[] column = image[y];
replicated[y] = new int[column.length * 2];
replicated[y + image.length] = new int[column.length * 2];
System.arraycopy(column, 0, replicated[y], 0, column.length);
System.arraycopy(column, 0, replicated[y], column.length, column.length);
System.arraycopy(column, 0, replicated[y + image.length], 0, column.length);
System.arraycopy(column, 0, replicated[y + image.length], column.length, column.length);
}
return replicated;
}
There might be some problems with this code since I wrote it not in front of a compiler, but I think it gets the point across.
Also, if this is for painting the image, you should consider using a TexturePaint instead.