Can I specify a dimension to copy in array.copy or any alternative?

Akshay Khade

I want to copy a 2D array into a second 2D array but only with the first dimension.

i.e. arrayA into arrayB but only arrayA[0] into arrayB[0].

Using a new method because using for it was too slow for my application. Is there any function or method in .NET or C# to achieve this?

Thanks in advance.

JustSomeGuy

From the documentation on Array.Copy

https://msdn.microsoft.com/en-us/library/y5s0whfd(v=vs.110).aspx

When copying between multidimensional arrays, the array behaves like a long one-dimensional array, where the rows (or columns) are conceptually laid end-to-end. For example, if an array has three rows (or columns) with four elements each, copying six elements from the beginning of the array would copy all four elements of the first row (or column) and the first two elements of the second row (or column). To start copying from the second element of the third row (or column), sourceIndex must be the upper bound of the first row (or column) plus the length of the second row (or column) plus two.

To copy one dimension of the array you would need to do something like this. Imagine that we have array1 and array2 that are two-dimensional and both have corresponding sizes.

So you just do something like Array.Copy(array1, array2, array1.GetLength(0)). Copying second dimension would be something like Array.Copy(array1, array1.GetLength(0), array2, array1.GetLength(1)) and so on.

If you want an explanation, imagine you have array that looks something like this. This is not a memory layout, just a representation as a table.

a[0,0] a[0, 1] ... a[0, n]
a[1,0] a[1, 1] ... a[1, n]
.     .     .
a[m, 0] a[m, 1] ... a[m, n]

So, Array.GetLength(0) is n + 1, Array.GetLength(1) is m + 1. And now imagine how it's laid out in memory.

It will look like this

a[0,0] a[0, 1] ... a[0, n] a[1,0] a[1, 1] ... a[1, n] ... a[m, 0] a[m, 1] ... a[m, n]

So, how with some calculations you can copy from any position to any by just imagining how it's laid out in memory.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

I can't copy the array

From Dev

Is there any copy update alternative command?

From Dev

How can I copy a Matlab array to a field of an array of structs

From Dev

How can I manipulate a copy of an array without changing the original array?

From Dev

Can I use copy to add data into a Postgres array datatype

From Dev

Why can't I copy a section of an array in java?

From Dev

How can I copy BitmapData into Byte array using C#?

From Dev

Can I use scp to copy files to the home directory without having to specify it?

From Dev

ValueError: cannot copy sequence with size 5 to array axis with dimension 2

From Dev

copy multi-dimension C array to Matlab mxArray type

From Dev

ValueError: cannot copy sequence with size 821 to array axis with dimension 7

From Dev

Android: How to create a copy of a 2 dimension array for use in another thread?

From Dev

Copy non-contiguous columns into into a multi-dimension array?

From Dev

Alternative for copy.com

From Dev

How can I make a new Array and copy all the postive elements from the other array in the new array and return it?

From Dev

Why can't I use std::copy in my copy constructor?

From Dev

Why can't I use std::copy in my copy constructor?

From Dev

Specify copy constructor to not be used as copy constructor

From Dev

Is there any problem if i copy ubuntu partition to any other partiotions?

From Dev

Is there any problem if i copy ubuntu partition to any other partiotions?

From Dev

How can I copy associative array items to another single array item recursively?

From Dev

How can I copy array from a Matlab struct array with fields to a struct of arrays

From Dev

How can I copy array data to an object that has an array and also a field?

From Dev

Perl : How can I convert an array reference to an array, without creating a copy

From Dev

Perl : How can I convert an array reference to an array, without creating a copy

From Dev

How can I copy array data to an object that has an array and also a field?

From Dev

I Want a Search Box With a Button Where I Can Copy Any Of The Image URL And I Want That Image To Be Displayed On The Same Web Page

From Dev

Is there any way to copy an array to the same array with less number of elements

From Dev

Can't copy string to an array of strings in C

Related Related

  1. 1

    I can't copy the array

  2. 2

    Is there any copy update alternative command?

  3. 3

    How can I copy a Matlab array to a field of an array of structs

  4. 4

    How can I manipulate a copy of an array without changing the original array?

  5. 5

    Can I use copy to add data into a Postgres array datatype

  6. 6

    Why can't I copy a section of an array in java?

  7. 7

    How can I copy BitmapData into Byte array using C#?

  8. 8

    Can I use scp to copy files to the home directory without having to specify it?

  9. 9

    ValueError: cannot copy sequence with size 5 to array axis with dimension 2

  10. 10

    copy multi-dimension C array to Matlab mxArray type

  11. 11

    ValueError: cannot copy sequence with size 821 to array axis with dimension 7

  12. 12

    Android: How to create a copy of a 2 dimension array for use in another thread?

  13. 13

    Copy non-contiguous columns into into a multi-dimension array?

  14. 14

    Alternative for copy.com

  15. 15

    How can I make a new Array and copy all the postive elements from the other array in the new array and return it?

  16. 16

    Why can't I use std::copy in my copy constructor?

  17. 17

    Why can't I use std::copy in my copy constructor?

  18. 18

    Specify copy constructor to not be used as copy constructor

  19. 19

    Is there any problem if i copy ubuntu partition to any other partiotions?

  20. 20

    Is there any problem if i copy ubuntu partition to any other partiotions?

  21. 21

    How can I copy associative array items to another single array item recursively?

  22. 22

    How can I copy array from a Matlab struct array with fields to a struct of arrays

  23. 23

    How can I copy array data to an object that has an array and also a field?

  24. 24

    Perl : How can I convert an array reference to an array, without creating a copy

  25. 25

    Perl : How can I convert an array reference to an array, without creating a copy

  26. 26

    How can I copy array data to an object that has an array and also a field?

  27. 27

    I Want a Search Box With a Button Where I Can Copy Any Of The Image URL And I Want That Image To Be Displayed On The Same Web Page

  28. 28

    Is there any way to copy an array to the same array with less number of elements

  29. 29

    Can't copy string to an array of strings in C

HotTag

Archive