Properly use a matrix double[,] in c# - c#

Im trying to pass as parameter this matrix:
double[,] array2 = new double[4, 5]
{
{ 45, -6, 8, -3, 48 },
{ 0, -56.733333, 3.6444444, -6.8666667, 26.8666667 },
{ 0, 0, 78.17111712, -15.2432, -133.378378},
{ 0, 0, 0, 94.190193, 319.457667}
};
But when I try to use it in the other function i get problems with out of range, I dont know what I'm doing wrong since I'm using the same range that I specified when I created the objetc double[,] Im using the next code:
For passing the matrix:
Example1 a = new Example1();
a.function(array2,4,5);
Using the function:
public double[] function(double[,] nn, int n, int m)
{
double[,] ma = new double[n, m];
ma = nn;
double[] x = new double[5] { 0, 0, 0, 0, 0 };
x[n] = ma[n, m] / ma[n, n];
return x;
}
Im getting the error when I try to use ma[n,m] / ma[n,n] And I dont know why is happenig cause ma[n,m] it happens to exist and ma[n,n] happens to exist to.

If in doubt, read the documentation
Arrays (C# Programming Guide)
Array Overview
An array has the following properties:
An array can be Single-Dimensional, Multidimensional or Jagged.
The number of dimensions and the length of each dimension are established when the array instance is created. These values can't be
changed during the lifetime of the instance.
The default values of numeric array elements are set to zero, and reference elements are set to null.
A jagged array is an array of arrays, and therefore its elements are reference types and are initialized to null.
Arrays are zero indexed: an array with n elements is indexed from 0 to n-1.
Array elements can be of any type, including an array type.
Array types are reference types derived from the abstract base type Array. Since this type implements IEnumerable and IEnumerable<T>, you
can use foreach iteration on all arrays in C#.
Some further reading
Multidimensional Arrays
Zero-based numbering
Why are zero-based arrays the norm?

The problem is that the matrix does not recognize double [4,5] cause when declarated goes from [0..3] taking 4 spaces and the [4,#] will take 5 spaces.

Related

In C#, how do you declare an array with all values set to zero, without explicitly declaring it?

What I am trying to do is something like this (in c#):
int[,] arr2d = {
{0, 0},
{0, 0},
{0, 0}
};
This produces an array that is 2 columns wide and 3 rows in length with all values set to zero.
What I am trying to do is something such that i can initialize an array where I give a value x for width, and a value y for height, which will declare a 2d array of those specifications of which all values are zero.
Essentially, how do I make a 2d array where all values are zero of any height and width WITHOUT declaring each one individually as in the code above.
0 is a default int value, so you can do the same by this code
var arr2d = new int[3,2];
If you want to fill an Array other than the default value, you can use Fill,
Array.Fill(arrayToFill, value)
To reset filled array to default value (eg: in int array to reset to 0)
Array.Clear(arrayToReset, 0, arrayToReset.Length);

Copying arrays in c#

I am trying to copy an array in C#.
The definition of the first array is :
byte [][] a which contains 8 arrays each of size 8192 bytes.
The destination array is a 1d array of size 8192*8
Buffer.BlockCopy(a,0,b,0,8192*8)
where b is the destination array. However I keep getting Object must be an array of primitives. Does anyone know why this is happening?
That's because you don't have a single array of value, you have an array of references to arrays of values.
The byte[][] type is not the same as the byte[,] type. The first is an array of arrays (aka jagged array) while the second is a two dimensional array.
You would need to copy each array by itself:
for (int i = 0; i < a.Length; i++) {
Buffer.BlockCopy(a[i], 0, b, 8192 * i, 8192);
}
That is because your first input array is not an array of primitives. It is an array of byte[] arrays.
Buffer.BlockCopy(a(array of byte[]) , 0, b (array of byte),0,8192*8)
Fix:
Buffer.BlockCopy(a.SelectMany().ToArray(), 0, b, 0, 8192*8)
But to be honest I think you have the same result with:
var b = a.SelectMany().ToArray();

Reading an array from an array

I'd like to pull a section of an array out for manipulation.
e.g. From an array that contains 50 items, i want to return items 12 to 22 in a new array
Im currently using LINQ which I am assuming is slow:
return fullArray.Skip(12).Take(22).ToArray();
Is there a quicker way?
The Array.Copy method is massively quicker than Linq (I've tested it before and it was 2 or 3 orders of magnitude quicker!)
var sourceArray = object[50];
var newArray = object[10];
// Copy 10 elements, starting at index 12, to newArray (starting at index 0)
Array.Copy(sourceArray, 12, newArray, 0, 10);
You can use Array.Copy Method (Array, Int32, Array, Int32, Int32) method;
Copies a range of elements from an Array starting at the specified
source index and pastes them to another Array starting at the
specified destination index. The length and the indexes are specified
as 32-bit integers.
For example;
int[] array1 = new int[50];
int[] array2 = new int[10];
Array.Copy(array1, 12, array2, array2.GetLowerBound(0), 10);

calling array in porting C# to PHP

I'm kinda new to C# and want to port some program from C# to PHP
one thing strange for me is,
double[][] rate = { new double[] { 1, 2, 3, 4, 5 },
new double[] { 2, 4, 6, 8, 10}
};
double[,,] quest = {{{1,2,3,4}, {1,1,2,3}, {2,3,4,5}},
{{2,3,4,5}, {1,1,1,1}, {3,3,3,3}}
};
for ( int i = 0; i < f.Length; i++ ){
dummy = rate[i][(int)quest[1, 1, 1] - 3];
}
my question is,
why can you call cube-array quest[1,1,1] this way, but then again why call array rate[i][blah] like in PHP?
do they work interchangeably? just a matter of expression , or something more than I know?
int[,,] is a multidimensional array. It's also called a rectangular array, as it's more like an X-dimensional rectangular block of ints (with X=3 in this case). Each slice, row, and column of the array has the same size as any other slice/row/column, respectively.
int[][][] is an array of arrays of arrays of ints. It's more treeish. It's also called a "jagged" array, because each array at a given index can have different lengths.
In many cases, the two can do the same things, but they each have their strengths. Rectangular arrays have a predictable "shape", and you don't have to be as paranoid about whether x[a, b, c] exists if a, b and c are within the bounds you set when you created the array. You also don't have to worry about creating arrays and subarrays for each level of the array. On the other hand, jagged arrays can be more flexible when it comes to resizing and expanding one part of the array without doing the whole thing.
As far as analogous structures go, a jagged array is closer to how PHP tends to treat arrays.
The first one is array-of-array and the another is Multidimensional array.
here is the link talk about this Arrays

Add a dimension to a multidimensional array in c#

I have a multidimensional array
byte[,] matrix;
and i want copy in a 3 dimension array
byte[,,] 3dplan;
in this way
3dplan[,,0]=matrix
What is the fastest way to accomplish this task in c#?
You need to manually copy the elements in a nested loop; there is no faster way.
If you switch to a jagged array (byte[,][] or byte[][][]), you can insert the smaller array as-is into a slot in the larger array (although they will both refer to the same array instance and will pick up changes)
You can copy 2d array into 3d array with Buffer.BlockCopy:
var _3dplan = new int[2, 2, 2];
var _2dplan = new int[2, 2] { { 1, 1 }, { 1, 1 } };
var index = 0;
var size = _2dplan.Length * sizeof(int);
Buffer.BlockCopy(_2dplan, 0, _3dplan, index * size, size);

Categories