-3

Given R = (R_1, R_2,..., R_n) how to obtain such n*(2n+2) array in C#, note that is array not List, and n is very large.

enter image description here

A.Oreo
  • 301
  • 3
  • 16
  • 1
    Did you have tried something? If not, then I'm sorry, but I have to give you a 0 for effort. Please don't expect that we would give you the code on a silver platter. – KarelG Apr 20 '17 at 07:18
  • Please read the intro to creating a [mcve] – Matt Evans Apr 20 '17 at 07:20
  • @DmitryEgorov There is the `Ri` row, then a row of ones and then two diagonal sqare matrices. I think `n*(2n+2)` is correct. – wkl Apr 20 '17 at 07:21
  • @MatthewEvans sorry, I am not familiar with the operations of array in C#, I just want to see a example, then I can quickly deal with the similar problem. – A.Oreo Apr 20 '17 at 07:27

1 Answers1

4

The following method describes your desired result:

double GetValue(int row, int col, double[]firstrow)
{
    if( row == 0)
        return firstrow[col];
    if( row == 1)
        return 1;
    if (row - 2 == col || row - firstrow.Length - 2 == col)
        return 1;
    else
        return 0;
}

You can loop over this method to fill your array:

var firstRow = new double[]{2.3, 4.3, 5.8};// example input
int n = firstRow.Length;
var result = new double[2*n+2, n]
for(int row = 0; row < 2*n+2; row++)
{
    for( int col = 0; col < n; col++) 
    {
        result[row, col] = GetValue(row,col,firstrow);
    }
}

However, if firstRow is big, the resulting n*(2n+2) may be too big to use. In this case you may want to replace the expanded array by a direct call to double GetValue(int row, int col, double[]firstrow) when you need a value.

wkl
  • 1,896
  • 2
  • 15
  • 26
  • Shouldn't `result[row][col] = ...` be `result[row, col] = ...`? – UnholySheep Apr 20 '17 at 07:37
  • @UnholySheep Yes, of course. I have corrected it. Thank you. – wkl Apr 20 '17 at 07:39
  • You seem to have reversed the `row` and `col` extents. There are `2*n+2` rows, and `n` columns, not the other way around. Maybe next time, actually _test_ code before you post it? Even when answering a question that shouldn't have been answered in the first place (i.e. a "gimme teh codz" question like this one), it's worth getting the answer right. – Peter Duniho Apr 20 '17 at 07:53
  • @PeterDuniho thank you, I fixed it. – wkl Apr 20 '17 at 07:56
  • so, we are not allowed to use assignment such as `result[0] = firstRow`? – A.Oreo Apr 20 '17 at 08:00
  • @A.Oreo Not with a multidimensional array. You can, if you use an array of arrays instead, see [here](http://stackoverflow.com/q/12567329/4011717) or [here](http://stackoverflow.com/q/597720/4011717) for more information. – wkl Apr 20 '17 at 08:07