Rotating Matrix - java

I have a matrixA like that
[0][1]
[2][3]
[4][5]
And after my custom rotation (different numbering) becomes matrixB like this:
[0][1][2]
[3][4][5]
What i want is to map the numberings of the matrix A to B when i look the matrixA from the left->right.
To explain: matrixA looking from the left->right looks like this.
[1][3][5]
[0][2][4]
And matrixB is as it is
[0][1][2]
[3][4][5]
So i want to map, preferably with an equation these values
1->0
3->1
5->2
0->3
2->4
4->5
The real matrix is much larger so please don't focus to the size of this matrix
If anyone has any suggestions to find an equation for this mappings or some other way to do the mapping described? i would appreciate it

Here is some code I use sometimes. This rotates the matrix by 90 or -90 degrees. This might be a start for your problem:
public int[][] rotateMatrixRight(int[][] matrix)
{
/* W and H are already swapped */
int w = matrix.length;
int h = matrix[0].length;
int[][] ret = new int[h][w];
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
ret[i][j] = matrix[w - j - 1][i];
}
}
return ret;
}
public int[][] rotateMatrixLeft(int[][] matrix)
{
/* W and H are already swapped */
int w = matrix.length;
int h = matrix[0].length;
int[][] ret = new int[h][w];
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
ret[i][j] = matrix[j][h - i - 1];
}
}
return ret;
}

Here you can find ten different formulas for your sequence 3,0,4,1,5,2.
Always consult OEIS when you need an integer sequence!

int height = 2, width = 3; // of matrix B
int [][] a = {{0,1}, {2, 3}, {4, 5}};
int [][] b = {{0,1,2}, {3,4,5}};
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
int b_val = b[i][j];
int a_val = a[j][height - i - 1];
map.put(a_val, b_val);
}
}
Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
Iterator<Map.Entry<Integer, Integer>> it = entries.iterator();
while(it.hasNext()) {
Map.Entry<Integer, Integer> e = it.next();
System.out.println(e.getKey() + " -> " + e.getValue());
}
See here in action.

Maybe something like this:
map(matrixA,x,y)
w=width of matrix A
h=height of matrix A
n=y*w+x
from=matrixA(x,y)
to=matrixA(n mod h, n / h)
return (from, to)
To make a map, just iterate over all x and y and make a mapping of all those variables.

in place C solution follows
void rotateRight(int matrix[][SIZE], int length) {
int layer = 0;
for (int layer = 0; layer < length / 2; ++layer) {
int first = layer;
int last = length - 1 - layer;
for (int i = first; i < last; ++i) {
int topline = matrix[first][i];
int rightcol = matrix[i][last];
int bottomline = matrix[last][length - layer - 1 - i];
int leftcol = matrix[length - layer - 1 - i][first];
matrix[first][i] = leftcol;
matrix[i][last] = topline;
matrix[last][length - layer - 1 - i] = rightcol;
matrix[length - layer - 1 - i][first] = bottomline;
}
}
}

Related

ImageJ - Cumulative histograms and Histograms

I have run into a small problem with my program as it seems unable to find the highest value in a histogram to calculate the scale the histogram is supposed to be so now the entire histogram is way out of bounds
I really hope someone can help me out since it's driving me crazy
import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.filter.*;
public class Oblig3_Oppg2 implements PlugInFilter {
public int setup(String arg, ImagePlus im) {;
return DOES_8G + NO_CHANGES;
}
public void run(ImageProcessor ip) {
final int W = 256;
final int H = 100;
final int H1 = 140;
int[] hist = ip.getHistogram();
int[] KH = new int[W]; //Cumulative Histogram Array
int maxVal;
//Calculates the highest pixel count in the Histogram
for (int i = 0; i < W; i++){
if (hist[i] > maxVal){
maxVal = i;
}
}
KH[0] = hist[0];
for(int i = 1; i < W; i++) {
KH[i] = KH[i-1] + hist[i];
}
ImageProcessor histIp = new ByteProcessor(W, H1);
histIp.setValue(255);
histIp.fill();
int max = KH[255];
for(int j = 0; j < W; j++){
KH[j] = (KH[j]*100)/max; //Scales the Cumulative Histogram
hist[j] = (hist[j]*100)/maxVal; // Scales the Histogram
}
for (int k = 0; k < W; k++){
histIp.setValue(0);
histIp.drawLine(k, H, k, H-KH[k]);
}
for (int k = 0; k < W; k++){
histIp.setValue(0);
histIp.drawLine(k, H, k, H-hist[k]);
}
for (int l = 0; l < W; l++){
histIp.setValue(l);
histIp.drawLine(l, 140, l, 102);
}
histIp.setValue(0);
histIp.drawLine(W, H, W, 0);
// Display the histogram image:
String hTitle = "Histogram";
ImagePlus histIm = new ImagePlus(hTitle, histIp);
histIm.show();
}
}
You should set maxVal to the actual value, not the current index in your loop:
for (int i = 0; i < W; i++){
if (hist[i] > maxVal){
maxVal = hist[i]; // <-- here
}
}
Furthermore, it might be better to limit the loop to hist.length instead of W. That would prevent errors in case you set W to some value different from the array length that ip.getHistogram() returns.
Since you don't provide a runnable example (i.e. the entire Java class; I assume you implement ij.plugin.filter.PlugInFilter), I didn't test the code, and it's not entirely clear to me what you want to achieve.

code does not work with arrays (multiple arrays in arraylist)

hi I'm having a little problem with arrays.
here's the code:
int frame_size = 410;
int frame_shift = 320;
ArrayList<double[]> frames = new ArrayList<double[]>();
for (int i = 0; i + frame_size < inbuf.length; i = i + frame_shift) {
double[] frame = new double[frame_size];
System.arraycopy(inbuf, i, frame, 0, frame_size);
frames.add(frame);
}
here I share a large array into several small, and add them to arraylist
I need to get more of ArrayList arrays and pass them to the function, and then accept the answer and assemble arrays processed one:
int[] Cover = new int[frames.size() * nParam];
for (int i = 0; i < frames.size(); i++) {
double[] finMc = Gos.getVek(frames.get(i));
for (int c = 0; c < finMc.length; c++) {
int mc = (int) finMc[c];
for (int m = 0; m < Cover.length; m++) {
Cover[m] = mc;
}
}
}
all this code does not work (
all elements of the array are zero Cover.
Сover[0] = 0
Cover[1] = 0
Cover[2] = 0
...
help solve the problem, please!)
thank you in advance)
Update
int frame_size = 410;
int frame_shift = 320;
ArrayList<double[]> frames = new ArrayList<double[]>();
for (int i = 0; i + frame_size < inbuf.length; i = i + frame_shift) {
double[] frame = new double[frame_size];
System.arraycopy(inbuf, i, frame, 0, frame_size);
frames.add(frame);
}
int[] Cover = new int[frames.size() * nParam];
for (int i = 0; i < frames.size(); i++) {
double[] finMc = Gos.getVek(frames.get(i));
for (int c = 0; c < finMc.length; c++) {
int mc = (int) finMc[c];
Cover[i * frames.size() + c] = (int) finMc[c];
}
}
Code^ not work(
UPDATE 2
double[] inbuf = new double[Size];
inbuf = toDoubleArray(Gos.data);
inbuf[2] = 10;
inbuf[4] = 14;
toDoubleArray
public static double[] toDoubleArray(byte[] byteArray) {
int times = Double.SIZE / Byte.SIZE;
double[] doubles = new double[byteArray.length / times];
for (int i = 0; i < doubles.length; i++) {
doubles[i] = ByteBuffer.wrap(byteArray, i * times, times)
.getDouble();
}
return doubles;
}
Code not work:
int frame_size = 410;
int frame_shift = 320;
ArrayList<double[]> frames = new ArrayList<double[]>();
for (int i = 0; i + frame_size < inbuf.length; i = i + frame_shift) {
double[] frame = new double[frame_size];
System.arraycopy(inbuf, i, frame, 0, frame_size);
frames.add(frame);
}
double[] Cover = new double[frames.size() * nParam];
for (int i = 0; i < frames.size(); i++) {
double[] finMc = Gos.getVek(frames.get(i));
for (int c = 0; c < finMc.length; c++) {
Cover[i * frames.size() + c] = finMc[c];
}
}
A couple of thoughts spring to mind immediately:
1)
for (int m = 0; m < Cover.length; m++) {
Cover[m] = mc;
}
This block starts m over at 0 every time through the loop. This means you're always writing over the same portion of the Cover array. So effectively, it's only the last frame's data that's stored. You probably meant
for(int m = i * frames.size(); m < (i+1)*frames.size(); i++) {
Cover[m] = mc;
}
But this raises a further issue -- you're writing the same value (mc) into the entire area allocated for a whole frame of data. You probably want to merge this loop with the previous loop so that this doesn't happen.
for (int c = 0; c < finMc.length; c++) {
Cover[i * frames.size() + c] = (int)finMc[c];
}
2) int mc = (int) finMc[c];
That line casts the value to an int which truncates the value stored at finMc[c]. If finMc[c] is between 0 and 1 this will yield 0 when the data is copied and casted. This is compounded by the previous issue which ensures that only the last frame's data ever gets copied. This is simply solved by removing the cast and declaring Cover as an array of doubles instead of ints.
So in sum, the code might work a bit better if it's written this way:
double[] Cover = new double[frames.size() * nParam];
for (int i = 0; i < frames.size(); i++) {
double[] finMc = Gos.getVek(frames.get(i));
for (int c = 0; c < finMc.length; c++) {
Cover[i * frames.size() + c] = finMc[c];
}
}

2D Array to Rectangles

Is there a way to parse 2 dimensional array like this into a rectangle object (x,y, width, height)?. I need the array of all possible rectangles...
{0,0,0,0,0}
{0,0,0,0,0}
{0,1,1,0,0}
{0,1,1,0,0}
{0,0,0,0,0}
This would give 4 rectangles (we are looking at 0):
0,0,5,2
0,0,1,5
3,0,2,5
0,5,5,1
I have tried something like this, but it only gives the area of the biggest rectangle...
public static int[] findMaxRectangleArea(int[][] A, int m, int n) {
// m=rows & n=cols according to question
int corX =0, corY = 0;
int[] single = new int[n];
int largeX = 0, largest = 0;
for (int i = 0; i < m; i++) {
single = new int[n]; // one d array used to check line by line &
// it's size will be n
for (int k = i; k < m; k++) { // this is used for to run until i
// contains element
int a = 0;
int y = k - i + 1; // is used for row and col of the comming
// array
int shrt = 0, ii = 0, small = 0;
int mix = 0;
int findX = 0;
for (int j = 0; j < n; j++) {
single[j] = single[j] + A[k][j]; // postions element are
// added
if (single[j] == y) { // element position equals
shrt = (a == 0) ? j : shrt; // shortcut
a = a + 1;
if (a > findX) {
findX = a;
mix = shrt;
}
} else {
a = 0;
}
}
a = findX;
a = (a == y) ? a - 1 : a;
if (a * y > largeX * largest) { // here i am checking the values
// with xy
largeX = a;
largest = y;
ii = i;
small = mix;
}
}
}// end of loop
return largeX * largest;
}
this code is working with 1s, but that is not the point right now

Matrix multiplication - single-dimension * multi-dimensional

I need to multiply two matrices. I understand pretty well how matrices work however in Java I am finding this a bit complex, so I researched a bit and found this.
public static int[][] multiply(int a[][], int b[][]) {
int aRows = a.length,
aColumns = a[0].length,
bRows = b.length,
bColumns = b[0].length;
int[][] resultant = new int[aRows][bColumns];
for(int i = 0; i < aRows; i++) { // aRow
for(int j = 0; j < bColumns; j++) { // bColumn
for(int k = 0; k < aColumns; k++) { // aColumn
resultant[i][j] += a[i][k] * b[k][j];
}
}
}
return resultant;
This code works fine. However the problem with this is that I need to multiply a single dimension matrix (1*5) by a multidimensional matrix (5*4), so the result will be (1*4) matrix and later on in the same program multiply a (1*4) matrix by a (4*3) matrix resulting in (1*3).
And I need to store the single dimension matrix in a normal array (double []) not multidimensional one!
I altered this code to the following but it still doesn't resolve the correct results.
public static double[] multiplyMatrices(double[] A, double[][] B) {
int xA = A.length;
int yB = B[0].length;
double[] C = new double[yB];
for (int i = 0; i < yB; i++) { // bColumn
for (int j = 0; j < xA; j++) { // aColumn
C[i] += A[j] * B[j][i];
}
}
return C;
Thanks in advance for any tips you may give :)
You can use RealMatrix to make it easier.
RealMatrix result = MatrixUtils.createRealMatrix(a).multiply(MatrixUtils.createRealMatrix(b));
double[] array = result.getRow(0);

Port Matlab's FFT to native Java

I want to port Matlab's Fast Fourier transform function fft() to native Java code.
As a starting point I am using the code of JMathLib where the FFT is implemented as follows:
// given double[] x as the input signal
n = x.length; // assume n is a power of 2
nu = (int)(Math.log(n)/Math.log(2));
int n2 = n/2;
int nu1 = nu - 1;
double[] xre = new double[n];
double[] xim = new double[n];
double[] mag = new double[n2];
double tr, ti, p, arg, c, s;
for (int i = 0; i < n; i++) {
xre[i] = x[i];
xim[i] = 0.0;
}
int k = 0;
for (int l = 1; l <= nu; l++) {
while (k < n) {
for (int i = 1; i <= n2; i++) {
p = bitrev (k >> nu1);
arg = 2 * (double) Math.PI * p / n;
c = (double) Math.cos (arg);
s = (double) Math.sin (arg);
tr = xre[k+n2]*c + xim[k+n2]*s;
ti = xim[k+n2]*c - xre[k+n2]*s;
xre[k+n2] = xre[k] - tr;
xim[k+n2] = xim[k] - ti;
xre[k] += tr;
xim[k] += ti;
k++;
}
k += n2;
}
k = 0;
nu1--;
n2 = n2/2;
}
k = 0;
int r;
while (k < n) {
r = bitrev (k);
if (r > k) {
tr = xre[k];
ti = xim[k];
xre[k] = xre[r];
xim[k] = xim[r];
xre[r] = tr;
xim[r] = ti;
}
k++;
}
// The result
// -> real part stored in xre
// -> imaginary part stored in xim
Unfortunately it doesn't give me the right results when I unit test it, for example with the array
double[] x = { 1.0d, 5.0d, 9.0d, 13.0d };
the result in Matlab:
28.0
-8.0 - 8.0i
-8.0
-8.0 + 8.0i
the result in my implementation:
28.0
-8.0 + 8.0i
-8.0
-8.0 - 8.0i
Note how the signs are wrong in the complex part.
When I use longer, more complex signals the differences between the implementations affects also the numbers. So the implementation differences does not only relate to some sign-"error".
My question: how can I adapt my implemenation to make it "equal" to the Matlab one?
Or: is there already a library that does exactly this?
in order to use Jtransforms for FFT on matrix you need to do fft col by col and then join them into a matrix. here is my code which i compared with Matlab fft
double [][] newRes = new double[samplesPerWindow*2][Matrixres.numberOfSegments];
double [] colForFFT = new double [samplesPerWindow*2];
DoubleFFT_1D fft = new DoubleFFT_1D(samplesPerWindow);
for(int y = 0; y < Matrixres.numberOfSegments; y++)
{
//copy the original col into a col and and a col of zeros before FFT
for(int x = 0; x < samplesPerWindow; x++)
{
colForFFT[x] = Matrixres.res[x][y];
}
//fft on each col of the matrix
fft.realForwardFull(colForFFT); //Y=fft(y,nfft);
//copy the output of col*2 size into a new matrix
for(int x = 0; x < samplesPerWindow*2; x++)
{
newRes[x][y] = colForFFT[x];
}
}
hope this what you are looking for. note that Jtransforms represent Complex numbers as
array[2*k] = Re[k], array[2*k+1] = Im[k]

Categories