Q:

C# LINQ use linq query write double for loop function

I want to write a function , in C++ Syntax:
double[Z][Y] Function (double[Z][Y] matrix){
    double Tem[Z][Y] ;
    for (int i; i<Z; i++){
        matrix[i][0] == 0 ? Tem[i][0] =1 :( 
        for (int j; j<Y; j++){
           if(matrix[i][j] > 0 ) Tem[i][j] = 1;
           elseif(matrix[i][j] < 0 ) Tem[i][j] = -1;
           else Tem[i][j] = Tem[i][j-1];
        })
    }
    return Tem;
} 

Now I want to translate into C# syntax with two dimensional List and linq instead of for loop.
But here is the problem that when we begin the querying  for each row, for case else Tem[i][j] = Tem[i][j-1];, Tem[i][j-1] is always the oringal value in matrix, actually we use the new Tem[i][j-1] to assign to Tem[i][j]. Anyway, can you show the correct solution?
What stump me is that  calling for the previous new value of Tem[i][j-1] but not the original value of  matrix[i][j-1].

A:

Although LINQ doesn't change the input elements (one of the commenters described it as "LINQ doesn't do assignments"), you can use LINQ to take one input element to create one output element. The function for this is Enumerable.Select. 
Select takes one object from your source collection and converts it into one object of your output collection. This output object may be of any type.
For LINQ an object of type double[][] can be considered as a sequence of sequences of doubles: your double[][] is easily converted:
double[][] matrix = ...
IEnumerable<IEnumerable<double>> mySequence = matrix;

Every element in mySequence is a sequence of doubles. For every element in mySequence you want to create an array of doubles, or to be more generic: you want to create a sequence of doubles: IEnumerable < double > This has the advantage that you can use deferred execution. If you really need an array you can always use ToArray() later.
Your problem is that you want to use the previous element from your sequence. Most LINQ functions only take the current input element from the enumerator.
Luckily, using Enumerable.Zip you can convert the innersequence to a sequence of tuples containing the current double and the previous double:
double[] innerSequence = ...
var tuples = innerSequence.Skip(1).Zip(innerSequence, 
    (x, y) => Tuple.Create(x,y)

Each tuple contains the current and the previous element of your innerSequence, except for the first element of the sequence, since the first one doesn't have a previous.
Continuing from code above where mySequence is a sequence of innerSequences and
each innerSequence is a sequence of doubles
var A = mySequence.Select(innerSequence =>
    // for every element in this sequence of doubles
    // you want the tuple of the element and its previous element
    outerElement.Skip(1).Zip(outerElement, (x,y) => Tuple.Create(x,y));

So now A is a sequence of tuple sequences, where each tuple is a combination of the current element and the previous element:
Every element i in your result is now an array of tuples.
Element [0] is now the array of tuples:

(Matrix[0][1], Matrix[0][0])
(Matrix[0][2], Matrix[0][1])
(Matrix[0][3], Matrix[0][2])
etc.

Element [1]:

(Matrix[1][1], Matrix[1][0])
(Matrix[1][2], Matrix[1][1])
etc.

var B = A.Select(tupleSequence => 
              tupleSequence.Select( tuple =>
                 // use tuple with element and previous element to decide which
                 // items to select

every tuple contains current element and previous element
          use this to create the double for the inner sequence)

