Toeplitz matrix initialization - java

I am trying to initialize a Toeplitz matrix in java. I want it to have this form
6 -4 1 0 0 ... 0
-4 6 -4 1 0 ... 0
1 -4 6 -4 1 ...0
................
0 ... 1 -4 6 -4 1
0 ... ...1 -4 6-4
0 .. ... 0 1 -4 6
I realized that the problem is in the if(j>i) in the bounds of data[i-j-1] . I tried to change it but i get the IndexOutOfBounds error. Here is the code i've written so far
int a1[][] = new int[size][size];
int data[] = new int[size];
data[0] = 6;
data[1] = -4;
data[2] = 1;
for(int i=3; i<size; i++){
data[i] = 0;
}
/* Creating the A1 matrix */
for(int i=0; i<size; i++)
{
for(int j=0; j<size; j++)
{
if(j>i){
a1[i][j] = data[j-i-1];
}else if(j==i){
a1[i][j] = data[0];
}else{
a1[i][j] = data[i-j-1];
}
}
}
And the output is
The Matrix is :
6 6 -4 1 0 0 0 0 0 0
6 6 6 -4 1 0 0 0 0 0
-4 6 6 6 -4 1 0 0 0 0
1 -4 6 6 6 -4 1 0 0 0
0 1 -4 6 6 6 -4 1 0 0
0 0 1 -4 6 6 6 -4 1 0
0 0 0 1 -4 6 6 6 -4 1
0 0 0 0 1 -4 6 6 6 -4
0 0 0 0 0 1 -4 6 6 6
0 0 0 0 0 0 1 -4 6 6

The problem is if i = j+1 or j = i+1, a1 is assigned a1[i][j] = data[0]. This is an off-by-one mistake, you should remove the 1:
for(int j=0; j<size; j++) {
if(j>i){
a1[i][j] = data[j-i];
}else if(j==i){
a1[i][j] = data[0];
}else{
a1[i][j] = data[i-j];
}
}

Related

Java DFS for adjacency matrix

I am working on a project to implement a DFS for a given adjacency matrix. I have the file reading in correctly and I have the program outputting a list of first encountered vertices, however, I am not getting the correct list. I am getting 1 2 6 5 7 3 4 8 but I need to get 1 2 6 7 4 3 5 8. I am not sure where I am going wrong, but any suggestions would help. Below I have attached the part of code the outputs the encountered vertices and the file with the adjacency matrix.
Code with the output:
public static void dfs(int vertex, boolean[] visitArray, ArrayList<Integer> DFSvertexList, ArrayList<Integer> DFSdeadEndList, int[][] DFStreeEdgeGraph, ArrayList<Integer> vertexList, ArrayList<Integer> iList, boolean[] visitArray2){
int ver = 0;
DFSvertexList.add(vertex);
visitArray[vertex] = true;
for(int i = 0; i <= dim-1; i++){
if(graph[vertex][i] == 1){
if(!visitArray[i]){
ver = i;
DFStreeEdgeGraph[vertex][i] = 1;
dfs(i, visitArray, DFSvertexList, DFSdeadEndList, DFStreeEdgeGraph, vertexList, iList, visitArray2);
DFSdeadEndList.add(i);
}
else if(i >= vertex && i != ver){
vertexList.add(vertex+1);
iList.add(i+1);
}
}
}
}
Here is the adjacency matrix:
0 1 0 0 1 1 0 0
1 0 0 0 0 1 1 0
0 0 0 1 0 0 1 0
0 0 1 0 0 0 0 1
1 0 0 0 0 1 0 0
1 1 0 0 1 0 0 0
0 1 1 0 0 0 0 1
0 0 0 1 0 0 1 0
Please let me know if more code is needed.

Increment individual values in array Java

I want to know how to increment certain indexes in an array.
int indexArrayHIS[] = new int[15];
int indexArrayFIX[] = new int[20];
indexArrayHIS[] is an array consisting out of index values:
6 9 9 17 0 19 16 1 0 7 1 18 16 8 10
I want fill indexArrayFIX[] with the number of each index instance. For example, there are 2 "9s" so at index 9 of indexArrayFIX[] I want to display a 2:
indexArrayFIX[] should output:
2 2 0 0 0 0 1 1 1 2 1 0 0 0 0 0 2 1 1 1 0
Hope this makes sense
Thanks
Why not just?
for (int i = 0; i < indexArrayHIS.length; i++) {
indexArrayFIX[indexArrayHIS[i]]++;
}

Reading a file with integers that may or may be delineated by spaces

So I have this file that is filled only with integers. I would like to, if possible, be able to read a file that may or may not have spaces delineating each integer.
Here are two visual examples.
The first one are integers that are not delineated by spaces while the second one are.
First Example:
020030090
000907000
900208005
004806500
607000208
003102900
800605007
000309000
030020050
Second Example:
0 3 8 0 12 0 15 16 6 0 4 0 0 0 0 0
0 11 5 0 1 0 0 14 13 0 3 9 12 7 0 0
0 0 0 0 6 0 0 0 0 12 0 14 0 0 0 16
10 16 0 6 2 13 0 0 0 8 7 0 0 0 0 0
3 10 1 0 13 0 0 15 0 9 0 16 5 0 0 0
0 0 16 0 0 0 0 11 14 0 13 12 0 3 0 0
4 0 7 8 0 0 12 9 0 0 0 0 0 0 11 0
0 6 0 0 16 0 0 0 11 5 0 0 15 0 0 2
11 0 0 12 0 0 8 2 0 0 0 1 0 0 14 0
0 7 0 0 0 0 0 0 3 11 0 0 8 16 0 9
0 0 13 0 3 6 0 7 16 0 0 0 0 11 0 0
0 0 0 2 5 0 14 0 15 0 0 4 0 13 7 1
0 0 0 0 0 14 5 0 0 0 16 2 13 0 8 10
14 0 0 0 8 0 9 0 0 0 0 11 0 0 0 0
0 0 6 15 7 1 0 3 12 0 0 13 0 2 5 0
0 0 0 0 0 15 0 12 1 14 0 3 0 6 16 0
Note:
I would also like to add that the second file might not be delineated by the same amount. This means that one integer could have one space after it and another integer could have 10 spaces after it.
What I have tried:
I have tried using the split("\s+") in combination with the replaceAll("", " ") but this not work in the second example because it would have more spaces and thus the split function would not work.
I have tried using replaceAll(" ", "") like this so that they have no spaces at all. Then I converted the string into a char array but that presented problems with integers greater than one digit(would not work with the second example as well).
Code:
public void initializeGrid(int grid[][], String fileName) throws FileNotFoundException, IOException
{
Scanner read = new Scanner(Paths.get(fileName));
int value;
for (int i = 0; i < ROWS; i++)
{
String line = read.nextLine();
String [] numbers = line.trim().split("\\s+");
for (int j = 0; j < COLUMNS; j++)
{
value = Integer.parseInt(numbers[j]);
grid[i][j] = value;
}
}
}
Following the recommendation of #dnault in the comments above, here's an implementation that uses the Java Collection framework instead of a 2d int array. This approach has an advantage over a 2d array in that the List for each row contains exactly as many entries as needed. Using arrays, if a line has less than COLUMN values, the array will contain zeros for all remaining values.
public List<List<Integer>> readFile(String fileName)
throws FileNotFoundException, IOException {
BufferedReader br = Files.newBufferedReader(Paths.get(fileName));
List<List<Integer>> values = new ArrayList<>();
for(String line; (line = br.readLine()) != null;){
String[] splitLine = line.trim().split("\\s+");
if(splitLine.length < 2)
values.add(parseSingleDigitValues(splitLine[0].toCharArray()));
else
values.add(parseDelimitedValues(splitLine));
}
return values;
}
private List<Integer> parseSingleDigitValues(char[] line) {
List<Integer> values = new ArrayList<>();
for(char c: line){
values.add(Integer.parseInt(String.valueOf(c)));
}
return values;
}
private List<Integer> parseDelimitedValues(String[] line) {
List<Integer> values = new ArrayList<>();
for(String str :line)
values.add(Integer.parseInt(str));
return values;
}
The resulting List<List<Integer>> can then be easily converted into a 2D int array using the following method:
private int[][] asArray(List<List<Integer>> lists){
int s1 = lists.size();
int s2 = 0;
for(List<Integer> sublist : lists){
if(sublist.size() > s2)
s2 = sublist.size();
}
int[][] arr = new int[s1][s2];
for(int i = 0; i < lists.size(); i++){
List<Integer> sublist = lists.get(i);
for(int j = 0; j < sublist.size(); j++){
arr[i][j] = sublist.get(j);
}
}
return arr;
}
EDIT In the end, if you clearly document your code/api then the burden is on the user to put it into proper use. I recommend you opt for simplicity in your API: Tell the user they must provide a space-delimited file. You can then provide a utility class that will convert a non-delimited file into a space-delimited file.

Copying 2d array elements from one to the other

I want to find adjugate matrix from a n x n matrix,
e.g the matrix is 4x4
{0, 11, 15, 11},
{7, 0, 1 , 8},
{4, 19, 0 , 6},
{2, 3, 5, 0}
This is my output
Matrix One
Loop: 1
0 1 8 19 0 6 3 5 0
Loop: 2
7 1 8 4 0 6 2 5 0
Loop: 3
7 0 8 4 19 6 2 3 0
Loop: 4
7 0 1 4 19 0 2 3 5
Loop: 5
11 15 11 19 0 6 3 5 0
Loop: 6
0 15 11 4 0 6 2 5 0
Loop: 7
0 11 11 4 19 6 2 3 0
Loop: 8
0 11 15 4 19 0 2 3 5
Loop: 9
11 15 11 0 1 8 3 5 0
Loop: 10
0 15 11 7 1 8 2 5 0
Loop: 11
0 11 11 7 0 8 2 3 0
Loop: 12
0 11 15 7 0 1 2 3 5
Loop: 13
11 15 11 0 1 8 19 0 6
Loop: 14
0 15 11 7 1 8 4 0 6
Loop: 15
0 11 11 7 0 8 4 19 6
Loop: 16
0 11 15 7 0 1 4 19 0
I stored the values into another 2darray(matrixTwo) to calculate the determinant to find the inverse matrix but found out that what is stored in my matrix two isn't the same as the output above.why is value stored different? please help
Matrix Two
Loop: 1
0 11 15 11 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 2
7 0 1 8 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 3
4 19 0 6 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 4
2 3 5 0 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 5
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 6
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 7
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 8
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 9
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 11
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 12
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 13
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 14
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 16
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
My Codes
public class Matrix {
public static void main(String argv[]) {
int matrix[][]= { {0, 11, 15, 11},
{7, 0, 1 , 8},
{4, 19, 0 , 6},
{2, 3, 5, 0}
};
int matrixTwo[][] = new int[(matrix.length) * (matrix.length)][(matrix.length) * (matrix.length)];
int ctr = 0;
System.out.println("Matrix One");
for(int i = 0; i < matrix.length; i++) {
for(int j = 0; j < matrix.length; j++) {
ctr++;
System.out.println("\nLoop: " + ctr);
for(int row = 0;row < matrix.length; row++) {
for(int col = 0; col < matrix[row].length; col++) {
if(row != i && col != j) {
System.out.print(matrix[row][col] + " ");
matrixTwo[row][col] = matrix[row][col];
}
}
}
}
}
int ctrTwo = 0;
System.out.println("\n\nMatrix Two");
for(int row = 0; row < matrixTwo.length; row++) {
ctrTwo++;
System.out.println("Loop: " + ctrTwo);
for(int col = 0; col < matrixTwo.length; col++) {
System.out.print(matrixTwo[row][col] + " ");
}
System.out.println();
}
}
These is different because they are printed in different orders
For a example,
From the matrix one
Loop: 1
0 1 8 19 0 6 3 5 0
prints
Loop: 1
matrix[1][1] = 0 matrix[1][2] = 1 matrix[1][3] = 8 matrix[2][1] = 19 matrix[2][2] = 0 matrix[2][3] = 6 matrix[3][1] = 3 matrix[3][2] = 5 matrix[3][3] = 0
From the matrixTwo
Loop: 1
0 11 15 11 0 0 0 0 0 0 0 0 0 0 0 0
prints
Loop: 1
matrixTwo[0][0] = 0 matrixTwo[0][1] = 11 matrixTwo[0][2] = 15 matrixTwo[0][3] = 11 matrixTwo[0][4] = 0 matrixTwo[0][5] = 0 matrixTwo[0][6] = 0 matrixTwo[0][7] = 0 matrixTwo[0][8] = 0 matrixTwo[0][9] = 0 matrixTwo[0][10] = 0 matrixTwo[0][11] = 0 matrixTwo[0][12] = 0 matrixTwo[0][13] = 0 matrixTwo[0][14] = 0 matrixTwo[0][15] = 0
Use System.out.print("matrix[" + row + "][" + col + "] = " + matrix[row][col] + " ");. You can see the difference
right now, you are simply copying the first matrix into the second.
here is the testclass where you can see it:
public class test2 {
int matrix[][] = { { 0, 11, 15, 11 }, { 7, 0, 1, 8 }, { 4, 19, 0, 6 },
{ 2, 3, 5, 0 } };
int matrixTwo[][] = new int[(matrix.length) * (matrix.length)][(matrix.length)
* (matrix.length)];
public test2() {
int ctr = 0;
System.out.println("Matrix One");
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix.length; col++) {
ctr++;
System.out.println("\nLoop: " + ctr);
for (int row1 = 0; row1 < matrix.length; row1++) {
for (int col1 = 0; col1 < matrix[row1].length; col1++) {
if (row1 != row && col1 != col) {
System.out.print(matrix[row1][col1] + " ");
matrixTwo[row1][col1] = matrix[row1][col1];
}
}
}
}
}
int ctrTwo = 0;
System.out.println("\n\nMatrix Two");
for (int row = 0; row < matrixTwo.length; row++) {
ctrTwo++;
System.out.println("Loop: " + ctrTwo);
for (int col = 0; col < matrixTwo.length; col++) {
System.out.print(matrixTwo[row][col] + " ");
}
System.out.println();
}
printArray(matrix);
System.out.println();
printArray(matrixTwo);
}
public void printArray(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.printf("%3d", matrix[i][j]);
}
System.out.println();
}
}
public static void main(String argv[]) {
new test2();
}
}
the result:
matrix:
0 11 15 11
7 0 1 8
4 19 0 6
2 3 5 0
matrixTwo:
0 11 15 11 0 0 0 0 0 0 0 0 0 0 0 0
7 0 1 8 0 0 0 0 0 0 0 0 0 0 0 0
4 19 0 6 0 0 0 0 0 0 0 0 0 0 0 0
2 3 5 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
this is not what you wanted to do.
think about what is done in this line: matrixTwo[row][col] = matrix[row][col];

How To Check For Diagonal Neighbors in a 2D Array and Set Current Value Equal to Number of Neighbors

I am trying to walk through a 2D array and count all diagonal neighbors that equal 0, then set the current position equal to the number of neighbors. For example:
0 5 0 9
0 5 0 3
1 9 4 6
7 0 0 9
Should change to
0 2 0 1
0 3 1 2
1 3 1 2
0 1 1 1
I am using the following code (I catch exceptions because there will be index out of bounds exceptions for all parameter numbers) :
int row, col, count;
count = 0;
// Standard for-loop used in walking through a 2D array
for (row = 0; row < NUM; row++) {
for (col = 0; col < NUM; col++) {
// Check top left neighbor
try {
if (grid[row - 1][col - 1] == 0) {
count++;
}
} catch (IndexOutOfBoundsException e) {
} // Check bottom left neighbor
try {
if (grid[row - 1][col + 1] == 0) {
count++;
}
} catch (IndexOutOfBoundsException e) {
} // Check top right neighbor
try {
if (grid[row + 1][col - 1] == 0) {
count++;
}
} catch (IndexOutOfBoundsException e) {
} // Check bottom right neighbor
try {
if (grid[row + 1][col + 1] == 0) {
count++;
}
} catch (IndexOutOfBoundsException e) {
} // Set current place in the array equal to number of 0 neighbors
grid[row][col]=count;
count = 0;
}
}
The issue is that my output is wrong. Instead of the supposed code, it changes to the following:
0 2 0 1
0 3 1 2
1 2 1 1
0 0 0 0
So the first two lines work , then the 3rd line has several OBO errors. Not sure what's even wrong with the last line, and I'm not sure where this is going wrong.
Summary
Original is:
0 5 0 9
0 5 0 3
1 9 4 6
7 0 0 9
That should change to:
0 2 0 1
0 3 1 2
1 3 1 2
0 1 1 1
But I'm getting:
0 2 0 1
0 3 1 2
1 2 1 1
0 0 0 0
Another Example Would Be:
Original:
5 0 0 3 9 5
0 0 9 5 3 0
0 0 0 9 7 3
7 0 5 0 9 5
0 0 3 0 0 0
9 5 0 3 7 0
Updated:
1 1 1 0 1 0
1 2 2 1 2 0
1 0 2 0 2 0
2 1 4 1 4 1
0 1 0 1 1 0
0 2 0 1 1 0
Any suggestions would be very much appreciated.
You start with
0 5 0 9
0 5 0 3
1 9 4 6
7 0 0 9
You start traversing from the top left and simultaneously modifying the matrix.
So after 2 rows have been modified, the intermediate matrix is
0 2 0 1
0 3 1 2
1 9 4 6
7 0 0 9
Now take into consideration the element at index [2][1]. In the original matrix, it had 3 zero neighbors, but this matrix only has 2, hence the difference in expected and obtained output.
Make a separate matrix to store the modified values.

Categories