如何将两行数组复制到另一行? [英] How do I copy two row of an array to another?

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

问题描述

你好,



我有一个以下形式的数组(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);

推荐答案

您必须遍历所有行并单独复制每个值。 Array.Copy不起作用,因为它适用于行而不是列。



这样的东西:

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:
int rows = tempdata.GetUpperBound(0);

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


首先,如果您使用的源数组和要复制到的目标数组, 具有相同的Rank(维度数),并且具有相同的Type,并且您想要复制Row或一些行数,然后您可以使用Array类中的各种'Copy Methods: [ ^ ]。



这是我过去使用的通用通用函数,用于获取行或列的副本(即,来自二维数组的数组的一个数组值与源数组的值相同的一维数组结果:
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:
// 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


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆