# Tag: matrix

## Java – generate and rotate matrix

recently I’m trying to learn Java a bit and currently, I’m working on a simple program that should generate a matrix and then rotate it. I’m stuck at the first part. What exactly is the problem? The logic of my code seems to be fine, but anyway program is returning not what I would expect. The code: Output: Please, enter

## error: constructor in class cannot be applied to given types; with matrix of generic variable in Java

In a first file I declared the Matrix class with corresponding generic variable and with a constructor defined as follows: And in a second test file class with main program I declare the matrix of generic variables with String elements. Nevertheless I don´t quite understand why I get the error: If I´m using a 2d-dimensional array what´s wrong with the

## Java multidimensional arrays – How to refer to the length of different dimensions

We are learning about 2D arrays in my AP computer science A class and I have a question that I’m struggling to find the clear answer to. For some context when I’m talking about my question, ragged arrays are out of the picture and when I refer to rows I am talking about arrays going across and when I refer

## Index of center of mxn matrix. if center does not exist, return the index of the square closest to the center with the highest integer

Trying to write a method which returns the center of the MxN matrix and if the center does not exist the method should return the index of the square closest to the center with the highest integer …

## How to swap rows and columns of a 2d array?

I’m trying to write a method for ‘transpose’ a two-dimensional array of integers, in which the rows and columns of the original matrix are exchanged. However, I have no idea how I can realize this. …

## Matrix multiplication in Java using 2D ArrayLists

I am trying to implement a simple matrix multiplication in Java, where I am storing the matrices in two-dimensional ArrayLists. It would seem the error is caused by the setting of matrix Result, inside the nested for loop, but I do not understand why. The code produces the result: when in fact it should be: Answer The Result is incorrectly

## How to check if contours are the same?

I’ve got two images, the second one is the first one after modification. I find contours of two images and then check if there any same contours. The problem is, that despite contours being the same (drawing contours of one image and another one gives totally same results) after checking that it never becomes true that one contour is equal

## Java: Inverse of a matrix using EJML not working as expected

Within a java project I’ve developed I need to calculate the inverse of a matrix. In order to align with other projects and other developers I’m using the Efficient Java Matrix Library (orj.ejml). For inverting the Matrix I’m using invert from org.ejml.ops.CommonOps, and I has worked fine until now that I’m getting a unexpected result I’ve isolated the case that

## How to get Cartesian product from multiple lists?

Say I have several List<T>s, I will put them into another list or other collections, so I don’t know how many list<T> I have until I call List<List<T>>.size() Take below List<Integer> as an example: How can I get the result of list1*list2*list3*…listn as a Cartesian product? For example: should be: Answer You can use recursion to achieve it, your base

## Transposing a matrix from a 2D array

I’m self teaching myself some java and I’m stuck on creating a 2D array that initializes it with random values and then creates the transpose of the array. An example output is: Original matrix Transposed matrix ^ Should be the final output. Some help with the code would appreciated! I would like to code to generate error messages if the