2009-07-25 2 views

Répondre

2

Je viens d'écrire ce code que je vais utiliser:

using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.Linq; 

namespace MiscellaneousUtilities 
{ 
    public static class Enumerable 
    { 
     public static T[,] ToRow<T>(this IEnumerable<T> target) 
     { 
      var array = target.ToArray(); 
      var output = new T[1, array.Length]; 
      foreach (var i in System.Linq.Enumerable.Range(0, array.Length)) 
      { 
       output[0, i] = array[i]; 
      } 
      return output; 
     } 

     public static T[,] ToColumn<T>(this IEnumerable<T> target) 
     { 
      var array = target.ToArray(); 
      var output = new T[array.Length, 1]; 
      foreach (var i in System.Linq.Enumerable.Range(0, array.Length)) 
      { 
       output[i, 0] = array[i]; 
      } 
      return output; 
     } 
    } 
} 
+0

@alexw Bonne prise. Je vais supprimer mon commentaire précédent. – shoelzer

8

Il n'y a aucun moyen direct. Vous devriez copier des choses dans un double[,]. En supposant que vous voulez dans une seule ligne:

double[,] arr = new double[1, original.Length]; 
for (int i = 0; i < original.Length; ++i) 
    arr[0, i] = original[i]; 
+0

@alexw Bonne prise. Je vais supprimer mon commentaire précédent. – shoelzer

0

Mehrdad suppose que la largeur est un car il n'y a aucun moyen de déterminer la largeur ou la hauteur d'un tableau à une dimension par lui-même. Si vous avez un (extérieur) notion de la 'largeur', alors le code de Mehrdad devient:

// assuming you have a variable with the 'width', pulled out of a rabbit's hat 
int height = original.Length/width; 
double[,] arr = new double[width, height]; 
int x = 0; 
int y = 0; 
for (int i = 0; i < original.Length; ++i) 
{ 
    arr[x, y] = original[i]; 
    x++; 
    if (x == width) 
    { 
     x = 0; 
     y++; 
    } 
} 

Bien que, rang majeur est probablement plus fréquente dans de nombreuses applications (matrices, des tampons de texte ou graphiques):

// assuming you have a variable with the 'width', pulled out of a rabbit's hat 
int height = original.Length/width; 
double[,] arr = new double[height, width]; // note the swap 
int x = 0; 
int y = 0; 
for (int i = 0; i < original.Length; ++i) 
{ 
    arr[y, x] = original[i]; // note the swap 
    x++; 
    if (x == width) 
    { 
     x = 0; 
     y++; 
    } 
} 
4

Si vous connaissez la largeur du tableau 2D, vous pouvez utiliser ce qui suit pour mettre le Valu es en une rangée après l'autre.

private T[,] toRectangular<T>(T[] flatArray, int width) 
    { 
     int height = (int)Math.Ceiling(flatArray.Length/(double)width); 
     T[,] result = new T[height, width]; 
     int rowIndex, colIndex; 

     for (int index = 0; index < flatArray.Length; index++) 
     { 
      rowIndex = index/width; 
      colIndex = index % width; 
      result[rowIndex, colIndex] = flatArray[index]; 
     } 
     return result; 
    } 
+1

Bien que très facile à suivre, il est (relativement) coûteux en calcul. – cjbarth

+0

Jon Skeet a publié une méthode axée sur la performance à http://stackoverflow.com/questions/5132397/fast-way-to-convert-a-two-dimensional-array-to-a-list-one-dimensional – ShawnFeatherly

Questions connexes