1

I have a function that works for vector inputs but I need to run this on a cube. My impulse is I need to slice a vector out of the cube and then run it over a double for loop.

This is the pseudo code:

public static double[,,] cubefunction(double[,,] input)
{
    int N = input.GetLength(0);
    var outputvector = new double[N,N,N];

    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            outputvector[1:N,i,j] = vectorfunction(input[1:N,i,j]);
        }
    }
    return outputvector;
}

Obviously, 1:N is matlab notation to grab the entire row. Is there an equivalent in C#? Or how would I go about this without triple looping?

nik
  • 1,672
  • 2
  • 17
  • 36
  • If you use jagged array then you can get the full row, but also you can add a third for loop with the missing coordinate – Gusman Apr 25 '14 at 13:52
  • of course triple looping is always an option but I was hoping to avoid this – nik Apr 25 '14 at 13:55
  • [This answer](http://stackoverflow.com/a/406502/3438854) has a LINQ-based solution that should generalize to your needs. – John C Apr 25 '14 at 14:05
  • @JohnC: does take skip work with more than one dimension? – nik Apr 25 '14 at 14:51
  • I was thinking it was more trivial, but you're right, LINQ doesn't naturally deal with multiple dimensions. You could write an extension method to convert to single-dimensional array (turned up [this answer](https://stackoverflow.com/questions/2615037/using-linq-to-extract-specific-values-from-multi-dimensional-array) with some searching) and query based on the indices...but the loop seems simpler, at that point. – John C Apr 25 '14 at 18:05
  • so my only option is a triple loop really? – nik Apr 26 '14 at 07:28

0 Answers0