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);
Related
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.
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();
I have a byte array . I need to remove the bytes at specific index and split the byte arrays. For example Say I have a byte array of length 1000 . And I need to remove the bytes from position 50 to 200 .So my expected result would be 2 byte arrays . One is 0-49 and another is 201-1000.
Is Array.RemoveAt the only way to remove the byte array with index?
Thanks in advance!
If speed is critical, you can create two new arrays and use Array.Copy() to copy the required bytes into them.
To make this easier to use, it might be more convenient to write a little extension method for arrays which extracts and returns a subset, like this (note: error handling omitted for brevity):
public static class ArrayExt
{
public static T[] Subset<T>(this T[] array, int start, int count)
{
T[] result = new T[count];
Array.Copy(array, start, result, 0, count);
return result;
}
}
Then you can use it like so (I have corrected the index from your example; you had it starting at 201, but it should have been 200):
var array1 = new byte[1000];
// ... populate array1 somehow, then extract subsets like so:
var array2 = array1.Subset( 0, 50);
var array3 = array1.Subset(200, 800);
// Now array2 and array3 are two byte arrays
// containing the required bytes.
This is probably the fastest you are likely to get.
You could use IEnumerable Take and Skip, for example
byte[] origin = new byte[200];
for(int i = 0; i < origin.Length; i++)
origin[i] = (byte)i;
byte[] first = origin.Take(50).ToArray();
byte[] second = origin.Skip(100).Take(50).ToArray();
Below code can be used to split the byte array, "index" starting index and "count" is the index count upto which index you want to split, here for your condition index=50 and count is 150;
List<byte> byteArray = array.ToList(); //array is the byte array
byteArray.RemoveRange(index, count);
Byte[] array1 = byteArray.ToArray(); //array1 is the resultant byte array
Cant you get the sub arrays like this?
byte[] array1 = array.ToList().GetRange(0, x1-1).ToArray();
byte[] array2 = array.ToList().GetRange(x2, array.Length- x2 - 1).ToArray();
where x1 and x2 is 50 and 200 in your example
Aside from looping. Is there an efficient way to instantiate a 2D array from multiple 1D arrays.
My 1D arrays must behave as column vectors in my 2D rectangular array. So Column 0 = First 1D array, Column 1 = Second 1D Array, etc...
I've also tried System.Buffer.BlockCopy, but block copy will roll the single 1D array into multiple columns which is not what I want.
Simple example of how I want to transform:
var c1 = new int[] {1, 2, 3, 4};
var c2 = new int[] {5, 6, 7, 8};
var result = new int[,]
{{1,5},
{2,6},
{3,7},
{4,8}};
Looping with unsafe code and pointers will be the fasted approach that C# can offer. Use unsafe because it eliminates the bounds checking which cannot be eliminated in this case b the current JIT.
Loop sequentially through the 2D array in memory order when writing. This means that you need to write row-by-row instead of column-by-column. This trick optimizes memory access and allows you to just increment the write pointer instead of calculating the address each time (y*width+y).
Aside from looping this will do(since you did not specify this as also a no solution):
var c1 = new int[] { 1, 2, 3, 4 };
var c2 = new int[] { 5, 6, 7, 8 };
var c3 = new int[,]
{
{c1[0],c2[0]},
{c1[1],c2[1]},
{c1[2],c2[2]},
{c1[3],c2[3]}
};
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