Click here to Skip to main content
15,902,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have an array (tempData) of the following form:
C#
double[,] tempData = new double[numDtPts, 8];

which is loaded from a txt file. This means to me as a matrix of n row and 8 columns. I would like to copy the first and the eights column to another array (y) as:
C#
private double[,] y = null;

C#
y = new double[numDtPts, 2];

The question is how to copy them to new array.

I am looking forward to your answers.
Thanks
ARMS

What I have tried:

I googled and found no direct answer. And as I am not a profi-programmer I didnt come to any reasonable answers.
The following code I use to copy all tempData to y but it is not possible to select which column I need.

C#
Array.Copy(tempData, 0, y, 0, numDtPts + 7);
Posted
Updated 17-Jun-16 20:36pm
v4
Comments
Dave Kreskowiak 17-Jun-16 13:15pm    
Your question doesn't make any sense. You want to copy the first row of values, which contains 8 values (or columns), to another array, which appears to only hold two values per row. That's doesn't make sense.

Also, what do you mean by the "eights row"?

Perhaps an example or what you want to do would clear things up?
ARMS_DEIR 17-Jun-16 13:19pm    
Hi Dave,

Thanks for your concern. I have corrected the question. I want to copy the first column of my first array to first column of the second array and again the eighth column of first array to the second column of the second array!

regards
AM

You have to loop through all the rows and copy each value individually. Array.Copy will not work as it works on rows, not colums.

Something like this:
C#
int rows = tempdata.GetUpperBound(0);

for (int index = 0; index <= rows; index++)
{
    y[index, 0] = tempdata[index, 0];
    y[index, 1] = tempdata[index, 7];
}
 
Share this answer
 
Comments
BillWoodruff 18-Jun-16 4:36am    
Array.Copy and its variations operate on some sequence of items in the in-memory sequential layout of the Array; that sequence may include "parts" of a row if the range of values being retrieved is not row-aligned.
Dave Kreskowiak 18-Jun-16 8:44am    
I know that but it's, IMHO, a bit more detailed of an explanation than the OP is prepared for.
First, if both the source array you use, and the destination array you intend to copy to, both have the same Rank (number of dimensions), and are of the same Type, and you want to copy a Row, or some number of Rows, then you can use the various 'Copy Methods in the Array Class: [^].

Here's a general purpose generic function I've used in the past to get a copy of either a Row, or a Column (i.e., a one-dimensional Array result of the same Type value as the source array's values), of an Array from a two-dimension Array:
C#
// bw: revised May, 2005
public T[] CopyRowOrColumn<T>(int ndx, T[,] sourceAry, bool isrow = true)
{
    T[] resultAry;

    int rank = sourceAry.Rank;

    if(sourceAry == null || rank != 2) throw new ArgumentException("requires a two-dimensional array as input that contains values");

    int rowdim = sourceAry.GetLength(0);
    int coldim = sourceAry.GetLength(1);

    if (isrow)
    {
        if(ndx > rowdim) throw new  ArgumentException("index greater than row dimension");
        resultAry = new T[coldim];;
    }
    else
    {
        if(ndx > coldim) throw new ArgumentException("index greater than column dimension");
        
        resultAry = new T[rowdim];
    }

    T value;

    for (int row = 0; row < rowdim; row++)
    {
        for (int col = 0; col < coldim; col++)
        {       
            value = sourceAry[row, col];

            if (isrow && row == ndx)
            {
                resultAry[col] = value;
            }
            else  if(col == ndx)
            {
                resultAry[row] = value;
            }
        }
    }

    return resultAry;
}
Usage sample:
var testrow = CopyRowOrColumn<double>(2, dblAry, true);
var testcol = CopyRowOrColumn<double>(2, dblAry, false)

I am going to assume you can use this, and easily adapt it to insert the values you copy from the source Array into your destination Array.

Good things to understand in theory, and practice, about Arrays:

1. often you can avoid a lot of "grunt" work by using generic Lists instead.

2. Array.Rank: the number of dimensions of an Array

3. the 'Array.GetLength method that returns the size of a specific Array dimension
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900