本文介绍了如何将两行数组复制到另一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我有一个以下形式的数组(tempData):

Hello,

I have an array (tempData) of the following form:

double[,] tempData = new double[numDtPts, 8];



从txt文件加载。这对我来说意味着n行和8列的矩阵。我想将第一列和第八列复制到另一个数组(y):


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:

private double[,] y = null;




y = new double[numDtPts, 2];



问题是如何将它们复制到新阵列。



我期待你的回答。

谢谢

ARMS



我是什么尝试过:



我用谷歌搜索,没有找到直接答案。因为我不是一个profi程序员,所以我没有得到任何合理的答案。

我使用以下代码将所有tempData复制到y,但是无法选择我需要的列。 />



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.

Array.Copy(tempData, 0, y, 0, numDtPts + 7);

推荐答案

int rows = tempdata.GetUpperBound(0);

for (int index = 0; index <= rows; index++)
{
    y[index, 0] = tempdata[index, 0];
    y[index, 1] = tempdata[index, 7];
}


// 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;
}

使用示例:

var testrow = CopyRowOrColumn<double>(2, dblAry, true);
var testcol = CopyRowOrColumn<double>(2, dblAry, false)



我假设您可以使用它,并轻松调整它以插入您从源数组复制到目标数组的值。



关于数组的理论和实践要理解的好事:



1.通常你可以通过使用通用列表来避免很多咕噜咕噜的工作。



2. Array.Rank:数字数组的维数



3.返回特定数组维度大小的Array.GetLength方法


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


这篇关于如何将两行数组复制到另一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 19:50