okay i have another question because as i told you i am trying to put all these values in result 3 as i mentioned before like this ( x , y ) then i have to get the max value of each point of this ( x , y ) for the whole points then get the min value of the maximum values , i managed to get the maximum values but when i try to get the minimum of them it turns shows zero.
This is the code.
for (int i = 0; i < result1.GetLength(0); i++)
{
for (int j = 0; j < result1.GetLength(1); j++)
{
for (int k = 0; k < result2.GetLength(0); k++)
{
for (int m = 0; m < result2.GetLength(1); m++)
{
result3[i, j] = result1[i, j] + "," + result2[k, m];
Console.WriteLine(result3[i, j]);
if (result1[i, j] > result2[k, m])
{
highestMoment[i, j] = result1[i, j];
}
else
{
highestMoment[i, j] = result2[k, m];
}
Console.WriteLine(highestMoment[i, j]);
if (lowestMoment[i, j] > highestMoment[i, j])
{
lowestMoment[i, j] = highestMoment[i, j];
}
Console.WriteLine(lowestMoment[i, j]);
counter++;
}
}
}
}
and this the whole code
double[,] Cranelocations = { { -12.3256, 0.5344 }, { -12.3256, -0.4656 }, { -12.3256, -1.4656 }, { -12.3256, -2.4656 } };
double[,] Picklocation = { { -0.3256, -3.4656 }, { 0.6744, -3.4656 }, { 1.6744, -3.4656 }, { 2.6744, -3.4656 }, { 3.6744, -3.4656 }, { 4.6744, -3.4656 }, { 5.6744, -3.4656 } };
double[,] Setlocation = { { 20.62, 5.03 }, { 24.28, 5.03 }, { 28.40, 5.03 }, { 32.11, 5.03 }, { 35.99, 5.26 }, { 40.18, 5.26 } };
double[] Weights = { 11.7865, 14.7335, 15.1015, 10.7465 };
double[,] result1 = new double[Weights.Length * Cranelocations.GetLength(0), Picklocation.GetLength(0)];
double[,] result2 = new double[Weights.Length * Cranelocations.GetLength(0), Setlocation.GetLength(0)];
object[,] result3 = new object[result1.GetLength(0), result1.GetLength(1)];
double[,] highestMoment = new double[result3.GetLength(0), result3.GetLength(1)];
double[,] lowestMoment = new double[highestMoment.GetLength(0), highestMoment.GetLength(1)];
int counter = 0;
for (int m = 0; m < Weights.Length; m++)
{
int offset = m * Cranelocations.GetLength(0);
for (int i = 0; i < Cranelocations.GetLength(0); i++)
{
for (int j = 0; j < Picklocation.GetLength(0); j++)
{
double x = Cranelocations[i, 0] - Picklocation[j, 0];
double y = Cranelocations[i, 1] - Picklocation[j, 1];
result1[i + offset, j] = Weights[m] * (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)));
}
}
}
//Console.WriteLine("-----------------------------------------------------------------");
for (int m = 0; m < Weights.Length; m++)
{
int offset = m * Cranelocations.GetLength(0);
for (int i = 0; i < Cranelocations.GetLength(0); i++)
{
for (int j = 0; j < Setlocation.GetLength(0); j++)
{
double x = Cranelocations[i, 0] - Setlocation[j, 0];
double y = Cranelocations[i, 1] - Setlocation[j, 1];
result2[i +offset, j] = Weights[m] * (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)));
//Console.WriteLine(result2[i, j]);
}
}
}
for (int i = 0; i < result1.GetLength(0); i++)
{
for (int j = 0; j < result1.GetLength(1); j++)
{
for (int k = 0; k < result2.GetLength(0); k++)
{
for (int m = 0; m < result2.GetLength(1); m++)
{
result3[i, j] = result1[i, j] + "," + result2[k, m];
Console.WriteLine(result3[i, j]);
if (result1[i, j] > result2[k, m])
{
highestMoment[i, j] = result1[i, j];
}
else
{
highestMoment[i, j] = result2[k, m];
}
Console.WriteLine(highestMoment[i, j]);
if (lowestMoment[i, j] > highestMoment[i, j])
{
lowestMoment[i, j] = highestMoment[i, j];
}
Console.WriteLine(lowestMoment[i, j]);
counter++;
}
}
}
}
You are never assigning lowestMoment before comparing it to highestMoment. Since the default value of a double is 0 then lowestMoment will always be lower than anything you compare it to giving your results of 0.
This might be what you're looking for:
for (int i = 0; i < result1.GetLength(0); i++)
{
int iOffset = i * result1.GetLength(1);
for (int j = 0; j < result1.GetLength(1); j++)
{
for (int k = 0; k < result2.GetLength(0); k++)
{
int kOffset = k * result2.GetLength(1);
for (int m = 0; m < result2.GetLength(1); m++)
{
result3[iOffset + j, kOffset + m] = result1[i, j] + "," + result2[k, m];
Console.WriteLine(result3[iOffset + j, kOffset + m]);
if (result1[i, j] > result2[k, m])
{
highestMoment[i, j] = result1[i, j];
}
else
{
highestMoment[i, j] = result2[k, m];
}
if (lowestMoment[i, j] == 0
|| lowestMoment[i, j] > highestMoment[i, j])
{
lowestMoment[i, j] = highestMoment[i, j];
}
Console.WriteLine(highestMoment[i, j]);
Console.WriteLine(lowestMoment[i, j]);
counter++;
}
}
}
}
Related
Hello. I have this task to sum the numbers as shown. Tried everything I can, but still not the right answer. Can I have some guidance?
static void Main(string[] args)
{
string input = Console.ReadLine();
int n = (int)char.GetNumericValue(input[0]);
int m = (int)char.GetNumericValue(input[2]);
int[,] matrix = new int[n, m];
int sum = 0;
//fill matrix
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
matrix[i, j] = (j * 3 + 1) + i * 3;
}
}
for (int i = 0; i < matrix.GetLength(0) - 1; i+=1)
{
for (int j = 0; j < matrix.GetLength(1) - i; j+=1)
{
if (i % 2 == 0)
{
sum += matrix[i, j + i] + matrix[i + 1, j + 1];
}
}
}
Console.WriteLine(sum);
}
I think you would've a easier time hard coding the input (and naming them as "columns" and "rows" instead, much more readable).
What is the expected output? Not sure I'm following this sum. I'm guessing, 297? If so:
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
Console.Write(matrix[i, j] + " ");
if(j == 5) Console.WriteLine();
if (matrix[i, j] % 2 != 0)
{
if (i == 0 || i == matrix.GetLength(0) - 1
|| j == 0 || j == matrix.GetLength(0))
{
sum += (matrix[i, j]);
}
else
{
sum += (matrix[i, j] * 2);
}
}
}
}
[i, j] add the biggest values of matrix to j the new array
its here:
But smallest not working..
Min values in "j" (not working)
for (int i = 0; i < olay; i++)
{
for (int j = 0; j < state; j++)
{
if (minimax[i] > matris[i, j])
{
minimax[i] = matris[i, j];
}
}
}
Console.WriteLine();
for (int i = 0; i < minimax.Length; i++)
{
Console.WriteLine(i + 1 + ". MINIMAX " + minimax[i]);
}
Console.ReadLine();
}
Max VALUES IN "J" (WORKING)
for (int i = 0; i < olay; i++)
{
for (int j = 0; j < state; j++)
{
if (maximax[i] < matris[i, j])
{
maximax[i] = matris[i, j];
}
}
}
/* Olaylar icin en yuksek State degerleri */
Console.WriteLine();
for (int i = 0; i < maximax.Length; i++)
{
Console.WriteLine(i + 1 + ". MAXIMAX " + maximax[i]);
}
Output console:
IMG Lınk: https://pasteboard.co/IKKQlet.jpg
The default values in your arrays are 0.
To calculate the minimum value in each column and write it to an array you can do something like this:
var resultArray = new int[rowLength];
var matrix = new int[rowLength, columnLength];
for (int i = 0; i < rowLength; i++)
{
// you have to set the first value as a minimum and after that compare
resultArray[i] = matrix[i, 0];
for (int j = 1; j < columnLength; j++)
{
if (matrix[i, j] < resultArray[i])
{
resultArray[i] = matrix[i, j];
}
}
}
i am trying the results which stored int previous arrays (1 and 2) in a new array 3 can show each result from array 1 with with each array 2, when i show the results from each one separately they are okay but when i try to mix them in array 3 i get the correct answers for the first value in array 1 with all values in array 2 then a lot of zeros appears.
here is the code for the array 3
for (int i = 0; i < result1.GetLength(0); i++)
{
for (int j = 0; j < result1.GetLength(1); j++)
{
for (int k = 0; k < result2.GetLength(0); k++)
{
for (int m = 0; m < result2.GetLength(1); m++)
{
result3[i, j] = result1[i, j] + "," + result2[k, m];
Console.WriteLine(result3[i, j]);
counter++;
}
}
}
}
and here is the whole code
double[,] Cranelocations = { { -12.3256, 0.5344 }, { -12.3256, -0.4656 }, { -12.3256, -1.4656 }, { -12.3256, -2.4656 } };
double[,] Picklocation = { { -0.3256, -3.4656 }, { 0.6744, -3.4656 }, { 1.6744, -3.4656 }, { 2.6744, -3.4656 }, { 3.6744, -3.4656 }, { 4.6744, -3.4656 }, { 5.6744, -3.4656 } };
double[,] Setlocation = { { 20.62, 5.03 }, { 24.28, 5.03 }, { 28.40, 5.03 }, { 32.11, 5.03 }, { 35.99, 5.26 }, { 40.18, 5.26 } };
double[] Weights = { 11.7865, 14.7335, 15.1015, 10.7465 };
double[,] result1 = new double[Weights.Length * Cranelocations.GetLength(0), Picklocation.GetLength(0)];
double[,] result2 = new double[Weights.Length * Cranelocations.GetLength(0), Setlocation.GetLength(0)];
object[,] result3 = new object[result1.GetLength(0) * result1.GetLength(1), result2.GetLength(0) * result2.GetLength(1)];
int counter = 0;
for (int m = 0; m < Weights.Length; m++)
{
for (int i = 0; i < Cranelocations.GetLength(0); i++)
{
for (int j = 0; j < Picklocation.GetLength(0); j++)
{
double x = Cranelocations[i, 0] - Picklocation[j, 0];
double y = Cranelocations[i, 1] - Picklocation[j, 1];
result1[i, j] = Weights[m] * (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)));
}
}
}
Console.WriteLine("-----------------------------------------------------------------");
for (int m = 0; m < Weights.Length; m++)
{
for (int i = 0; i < Cranelocations.GetLength(0); i++)
{
for (int j = 0; j < Setlocation.GetLength(0); j++)
{
double x = Cranelocations[i, 0] - Setlocation[j, 0];
double y = Cranelocations[i, 1] - Setlocation[j, 1];
result2[i, j] = Weights[m] * (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)));
}
}
}
for (int i = 0; i < result1.GetLength(0); i++)
{
for (int j = 0; j < result1.GetLength(1); j++)
{
for (int k = 0; k < result2.GetLength(0); k++)
{
for (int m = 0; m < result2.GetLength(1); m++)
{
result3[i, j] = result1[i, j] + "," + result2[k, m];
Console.WriteLine(result3[i, j]);
counter++;
}
}
}
}
}
For each m you reset i to 0 and start writing at the beginning of your array again.
You need to keep moving the index forward when you increment m.
The same is when you are combining the two indexes. When you are combining two iterations to get an index you generally multiply the first by the length of the second, then add them together.
for (int m = 0; m < Weights.Length; m++)
{
int offset = m * Cranelocations.GetLength(0);
for (int i = 0; i < Cranelocations.GetLength(0); i++)
{
for (int j = 0; j < Picklocation.GetLength(0); j++)
{
double x = Cranelocations[i, 0] - Picklocation[j, 0];
double y = Cranelocations[i, 1] - Picklocation[j, 1];
result1[i + offset, j] = Weights[m] * (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)));
}
}
}
Console.WriteLine("-----------------------------------------------------------------");
for (int m = 0; m < Weights.Length; m++)
{
int offset = m * Cranelocations.GetLength(0);
for (int i = 0; i < Cranelocations.GetLength(0); i++)
{
for (int j = 0; j < Setlocation.GetLength(0); j++)
{
double x = Cranelocations[i, 0] - Setlocation[j, 0];
double y = Cranelocations[i, 1] - Setlocation[j, 1];
result2[i + offset, j] = Weights[m] * (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)));
}
}
}
for (int i = 0; i < result1.GetLength(0); i++)
{
int iOffset = i * result1.GetLength(1);
for (int j = 0; j < result1.GetLength(1); j++)
{
for (int k = 0; k < result2.GetLength(0); k++)
{
int kOffset = k * result2.GetLength(1);
for (int m = 0; m < result2.GetLength(1); m++)
{
result3[iOffset + j, kOffset + m] = result1[i, j] + "," + result2[k, m];
Console.WriteLine(result3[iOffset + j, kOffset + m]);
counter++;
}
}
}
}
I need help for 1-level 5/3 discrete Haar wavelet transform (DWT) source code with c# .
I use this project, and the methods of forward wavelet transform is here :
FWT(double[] data)
{
int h = data.Length >> 1;
for (int i = 0; i < h; i++)
{
int k = (i << 1);
temp[i] = data[k] * s0 + data[k + 1] * s1;
temp[i + h] = data[k] * w0 + data[k + 1] * w1;
}
}
FWT(double[,] data)
{
for (int k = 0; k < 1; k++)
{
for (int i = 0; i < rows / (k+1); i++)
{
for (int j = 0; j < row.Length / (k+1); j++)
row[j] = data[i, j];
FWT(row);
for (int j = 0; j < row.Length / (k+1); j++)
data[i, j] = row[j];
}
for (int j = 0; j < cols / (k+1); j++)
{
for (int i = 0; i < col.Length / (k+1); i++)
col[i] = data[i, j];
FWT(col);
for (int i = 0; i < col.Length / (k+1); i++)
data[i, j] = col[i];
}
}
}
w0 = 0.5; w1 = -0.5;s0 = 0.5;s1 = 0.5;
I searched about this topic in the papers , but I don't understand the algorithm of 5/3 or 9/7 wavelet filters and how can I change this code?
Any help would be much appreciated
You may find it useful: implementation of jpeg2000 decoder in pdf.js.
The implementation of the core of the 5-3 code:
function reversibleTransformFilter(x, offset, length) {
var len = length >> 1;
offset = offset | 0;
var j, n;
for (j = offset, n = len + 1; n--; j += 2) {
x[j] -= (x[j - 1] + x[j + 1] + 2) >> 2;
}
for (j = offset + 1, n = len; n--; j += 2) {
x[j] += (x[j - 1] + x[j + 1]) >> 1;
}
};
I need to calculate matrix: ( X^(T) * X )^(-1).
Legend for the code&comments:
x is double[,] array;
xT - transposed matrix
^(-1) - inverted matrix
Every time i generate new random matrix to work with it and i found out that program is very unstable, because it isn't working properly with any input data. I'm sure about that because i need to get Identity matrix in the end if everything's fine, but sometimes i get a totally terrible Ineverted matrix so i don't get an Identity matrix. I'm dissappointes because i always use the same type of data and do not convert anything. Compiler is MVS 2010. Hope You will help me.
Here is my Program.cs:
static void Main(string[] args)
{
Matrix x = new Matrix(5, 4);
//Matrix temp = new Matrix(x.Row, x.Col);
//double[] y = new double[x.Row];
//double[] b = new double[x.Row];
//this data isn't calculated correctly. used for debugging
x.MatrixX[0, 0] = 7; x.MatrixX[0, 1] = 6; x.MatrixX[0, 2] = 5; x.MatrixX[0, 3] = 8;
x.MatrixX[1, 0] = 7; x.MatrixX[1, 1] = 5; x.MatrixX[1, 2] = 8; x.MatrixX[1, 3] = 5;
x.MatrixX[2, 0] = 6; x.MatrixX[2, 1] = 8; x.MatrixX[2, 2] = 6; x.MatrixX[2, 3] = 8;
x.MatrixX[3, 0] = 8; x.MatrixX[3, 1] = 5; x.MatrixX[3, 2] = 8; x.MatrixX[3, 3] = 7;
x.MatrixX[4, 0] = 8; x.MatrixX[4, 1] = 5; x.MatrixX[4, 2] = 6; x.MatrixX[4, 3] = 7;
/*
7,00000 6,00000 5,00000 8,00000
7,00000 5,00000 8,00000 5,00000
6,00000 8,00000 6,00000 8,00000
8,00000 5,00000 8,00000 7,00000
8,00000 5,00000 6,00000 7,00000
*/
//random matrix generation
/*
Random rnd = new Random();
for (int i = 0; i < x.Row; i++)
for (int j = 0; j < x.Col; j++)
x.MatrixX[i, j] = rnd.Next(5, 10);
*/
/*i'm going to calculate: ( X^(T) * X )^(-1)
* 1. transpose X
* 2. multiply X and (1)
* 3. invert matrix (2)
* +4. i wanna check the results: Multilate of (2) and (3) = Identity_matrix.
* */
Matrix.Display(x);
//1
Matrix xt = Matrix.Transpose(x);
Matrix.Display(xt);
//2
Matrix xxt = Matrix.Multiply(x, xt);
Matrix.Display(xxt);
//3
Matrix xxtinv = Matrix.Invert(Matrix.Multiply(x, xt));
Matrix.Display(xxtinv);
//4
Console.WriteLine("Invert(xxt) * xxt. IdentityMatrix:");
Matrix IdentityMatrix = Matrix.Multiply(xxtinv, xxt);
Matrix.Display(IdentityMatrix);
Console.ReadKey();
}
And here is Matrix.cs with all functions:
public class Matrix
{
private double[,] matrix;
private int row;
private int col;
#region constructors
public Matrix(int Row, int Col)
{
this.row = Row;
this.col = Col;
matrix = new double[Row, Col];
}
public Matrix()
{
Random rnd = new Random();
Row = rnd.Next(3, 7);
Col = rnd.Next(3, 7);
matrix = new double[Row, Col];
for (int i = 0; i < Row; i++)
for (int j = 0; j < Col; j++)
matrix[i, j] = rnd.Next(5, 10);
}
public Matrix(Matrix a)
{
this.Col = a.Col;
this.Row = a.Row;
this.matrix = a.matrix;
}
#endregion
#region properties
public int Col
{
get { return col; }
set { col = value; }
}
public int Row
{
get { return row; }
set { row = value; }
}
public double[,] MatrixX
{
get { return matrix; }
set { matrix = value; }
}
#endregion
static public Matrix Transpose(Matrix array)
{
Matrix temp = new Matrix(array.Col, array.Row);
for (int i = 0; i < array.Row; i++)
for (int j = 0; j < array.Col; j++)
temp.matrix[j, i] = array.matrix[i, j];
return temp;
}
static public void Display(Matrix array)
{
for (int i = 0; i < array.Row; i++)
{
for (int j = 0; j < array.Col; j++)
Console.Write("{0,5:f2}\t", array.matrix[i, j]);
Console.WriteLine();
}
Console.WriteLine();
}
static public Matrix Multiply(Matrix a, Matrix b)
{
if (a.Col != b.Row) throw new Exception("multiplication is impossible: a.Col != b.Row");
Matrix r = new Matrix(a.Row, b.Col);
for (int i = 0; i < a.Row; i++)
{
for (int j = 0; j < b.Col; j++)
{
double sum = 0;
for (int k = 0; k < b.Row; k++)
sum += a.matrix[i, k] * b.matrix[k, j];
r.matrix[i, j] = sum;
}
}
return r;
}
static public Matrix Invert(Matrix a)
{
Matrix E = new Matrix(a.Row, a.Col);
double temp = 0;
int n = a.Row;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
E.matrix[i, j] = 0.0;
if (i == j)
E.matrix[i, j] = 1.0;
}
for (int k = 0; k < n; k++)
{
temp = a.matrix[k, k];
for (int j = 0; j < n; j++)
{
a.matrix[k, j] /= temp;
E.matrix[k, j] /= temp;
}
for (int i = k + 1; i < n; i++)
{
temp = a.matrix[i, k];
for (int j = 0; j < n; j++)
{
a.matrix[i, j] -= a.matrix[k, j] * temp;
E.matrix[i, j] -= E.matrix[k, j] * temp;
}
}
}
for (int k = n - 1; k > 0; k--)
{
for (int i = k - 1; i >= 0; i--)
{
temp = a.matrix[i, k];
for (int j = 0; j < n; j++)
{
a.matrix[i, j] -= a.matrix[k, j] * temp;
E.matrix[i, j] -= E.matrix[k, j] * temp;
}
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
a.matrix[i, j] = E.matrix[i, j];
}
return a;
}
}
In your example, the determinant of x * transpose(x) is zero. As a result, there is no inverse, which is probably why you're getting strange results.
I also note that your Inverse function modifies the matrix passed to it. This should probably be modified to avoid that.