Skip to main content
 首页 » 编程设计

c#之如何用更少的代码行简洁地将 ListT> 中的值提取到 M x 1 double[,] 数组中

2024年02月20日12TianFang

是否可以从 List<T> 中提取值将 M x 1 double[,] 数组简洁地放入会不会减少代码行数?

我有一个类型定义为:

public class Trajectory 
{ 
    public Vector3 Position { get; set; } 
    // ... more codes 
} 

Vector3 定义为:

    public struct Vector3 
    { 
        public float X; 
        public float Y; 
        public float Z; 
 
        public Vector3(float x, float y, float z) 
        { 
            X = x; 
            Y = y; 
            Z = z; 
        } 
        // ... more vector3 operators 
     } 

目前我有 List<Trajectory> trajectory .它最多有 80 个条目。我只想存储 trajectory 中每个条目的 X、Y、X 值作为 240 x 1 double[,]数组(按 X、Y、Z 值的顺序)。

我当前的解决方案相当冗长且丑陋。开始了:

            // take a snapshot of current trajectory 
            List<Entry> tempEntry = new List<Entry> (Entries); 
 
            // create a temporary vector3Values 
            List<Vector3> vector3Values = new List<Vector3>(); 
 
            foreach (Entry e in tempEntry) 
            { 
                vector3Values.Add(new Vector3(e.Position.X, e.Position.Y, e.Position.Z)); 
            }  
 
            /* Start an index at 0. 
             * This is for foreach iteration to extract the value of x, y, and z from each vector3 
             */ 
            int index = 0; 
 
            // find the size of the list, in case max limit is changed 
            int listCount = inputVector3.Count; 
 
            /* set the length of the new array by multiplying the size of the list by 3. 
             * We want: 
             * [x1 x2 x3...xn y1 y2 y3...yn z1 z2 z3...zn]'. 
             * Therefore, the size of the reshaped array is three times of the original array             * 
             */ 
            int maxRowLength = listCount * 3; 
 
            // create double[,] variable to store the reshaped data, three times the length of the actual list.  
            double[,] result = new double[maxRowLength, 1]; 
 
            // start going for each vector, then store the x components in the double[,] array. 
            foreach (Vector3 vector3 in inputVector3) 
            { 
                result[index, 0] = vector3.X; 
                index++; 
            } 
 
            /* continuing from the previous index value, start going for each vector,  
             * then store the z components in the double[,] array. 
             */ 
            foreach (Vector3 vector3 in inputVector3) 
            { 
                result[index, 0] = vector3.Y; 
                index++; 
            } 
 
            /* continuing from the previous index value, start going for each vector,  
             * then store the z components in the double[,] array. 
             */ 
            foreach (Vector3 vector3 in inputVector3) 
            { 
                result[index, 0] = vector3.Z; 
                index++; 
            } 

最终我得到了我想要的。 M x 1 双 [,] 数组。我使用 double[,] 与我此时需要的 Matlab 的 MWArray 对象进行互操作。

那么,问题是,是否有一种简洁的方法来完成我在这里所做的事情?

已编辑:这种转换每秒需要多次(感谢 Chris Sinclair 提出这个问题),但是,目前这不是问题。

请您参考如下方法:

你可以做类似下面的事情来避免循环三次:

var result = new double[entries.Count * 3, 1]; 
 
for (int i = 0; i < entries.Count; i++) 
{ 
    result[i, 0] = entries[i].Position.X; 
    result[i + entries.Count, 0] = entries[i].Position.Y; 
    result[i + entries.Count * 2, 0] = entries[i].Position.Z; 
}