Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
2.59/5 (3 votes)
See more:
I am trying to fill empty cells in the 2D array. I want to fill rows and columns in the border of a 2D array.

Eg My actual 2D array like this

C#
0 0 0 0 0 0
0 2 3 3 2 0
0 2 3 4 2 0
0 3 2 3 1 0
0 4 3 4 2 0
0 0 0 0 0 0


In this I want to fill those zeros with some values. I am trying to an extrapolation method to fill this. But I am not getting how to do this in my 2D array.


I want to generate an (N+2) by (N+2) array from NbyN matrix using extrapolation.

What I have tried:

I tried to average of nearest and searched in net for extrapolation algorithm to fill the empty cell in border.
Posted
Updated 28-Mar-16 4:24am
v2
Comments
Patrice T 28-Mar-16 2:09am    
what have you done ?
show code
what do you mean by "average of nearest" and "extrapolation algorithm" ?
Sergey Alexandrovich Kryukov 28-Mar-16 2:19am    
First of all, none of your cells are "empty". Without your explanation of "extrapolation", the problem does not make certain sense. You can use simple linear extrapolation, but who knows what you really need?
—SA

Please see my comment to the question.

The simplest linear extrapolation algorithm is, well… simple.

First of all, none of your cells are empty. Let's assume that 0 cells are "empty", but you should not denote them with 0, because you have to make difference between 0 as a valid number and "no value". Anyway… For example, let's consider second horizontal line "0 2 3 3 2 0". Let me show it on a graph, using some pseudo-graphics:
    | |
  | | | |
  | | | |
0 1 2 3 4 5
Here, horizontal row of number represent the index of a cell in a horizontal row, and | bars represent the value in each cell. Now, linear extrapolation means that you draw a straight line connecting points on a graph, and continue the leftmost and rightmost line segment to intersect with X axes. Actually, it makes the simple linear equation
f(x) = A + B*x

You have two points, f(1)=2 and f(2)=3, so the solution of the equation gives you f(0)=1.
Likewise, f(5)=1, determined by two values, f(3) and f(4). As you can see, only two leftmost points are involved in determination of f(0), and only two rightmost points — in determination of f(5).

The same way, find out the values for each row, and, in the same way, for each column.

—SA
 
Share this answer
 
v2
Comments
Sascha Lefèvre 28-Mar-16 4:25am    
+5
Sergey Alexandrovich Kryukov 28-Mar-16 10:36am    
Thank you, Sascha.
—SA
If you wish to manipulate the "edge cells" of a 2 dimensional Array of objects of various Types, here's one strategy:
C#
// required
using System.Collections.Generic;
using System.Linq;

public void Set2DArrayValues<T>(ref T[,] theAry, Func<int, int, T> arrayValueSetter)
{
    if (theAry == null) return;
    if (xfunc == null) return;

    int dim0 = theAry.GetLength(0);
    if (dim0 == 0) return;

    int dim1 = theAry.GetLength(1);
    if (dim1 == 0) return;

    for (int i = 0; i < dim0; i++)
    {
        for (int j = 0; j < dim1; j++)
        {
            theAry[i,j] = arrayValueSetter(i, j);
        }
    }
}
This a generic Method which gets passed a 2-dimensional Array of arbitrary Type (T), and a Func (Function) that takes two integer arguments, and returns an object of Type T.

Here's how you could use it to create a 6x6 Array of String with zeroes in all the "edge" cells, and the non-edge cells set to String.Empty:
C#
private strAry[,];

private void SampleUsage()
{
    // initialize
    strAry = new string[6,6];

    // get the array bounds
    int dim0 = strAry.GetLength(0) - 1;
    int dim1 = strAry.GetLength(1) - 1;

    // define a transformation function
    Func<int, int, string> stringValueSetter = (i1, i2) =>
    {
        return (i1 == 0 || i1 == dim0 || i2 == 0 || i2 == dim1)
            ? "0"
            : String.Empty;
    };

    // uses pre-defined Func 'stringValueSetter
    // sets the edge cells to "0"; the non-edge cells to String.Empty
    Set2DArrayValues<string>(ref strAry, stringValueSetter);
}

// output (commas inserted by code now shown here)
//0,	0,	0,	0,	0,	0,	
//0,	,	,	,	,	0,	
//0,	,	,	,	,	0,	
//0,	,	,	,	,	0,	
//0,	,	,	,	,	0,	
//0,	0,	0,	0,	0,	0
Another example: if we had created an Integer 2-d array, and wanted the diagonals in the matrix to be the square of the matrix row number, and all other cells to be set to #0:
int[,] intAry = new int[6,6];

// note we don't use the Array bounds here

Set2DArrayValues<int>(ref intAry, (i1, i2) =>
{
    return (i1 == i2)
        ? i1*i2 // arbitrary computation
        : 0;
});
Note that here we define the necessary Func (function) directly by using a lambda expression.

But, what if we want to do more than just "set" values; what if we'd like to transform existing values in the Array? To do that requires only a minor modification to the above:
C#
public void Transform2DArrayValues<T>(ref T[,] theAry, Func<T, int, int, T> xtfunc)
{
    if (theAry == null) return;
    if (xtfunc == null) return;

    int dim0 = theAry.GetLength(0);
    if (dim0 == 0) return;

    int dim1 = theAry.GetLength(1);
    if (dim1 == 0) return;

    for (int i = 0; i < dim0; i++)
    {
        for (int j = 0; j < dim1; j++)
        {
            theAry[i, j] = xtfunc(theAry[i,j], i, j);
        }
    }
}
Now, the Func (function) we pass will contain the current value in the cell; we can use that in some way, or ignore it. For example:
C#
int dim0 = 6;
int dim1 = 6;

string[,] strAry2 = new string[dim0,dim1];

strAry2[1, 2] = "Bacon";
strAry2[3, 4] = "Bacon";

// note we do not use the Array bounds here

Func<string, int, int, string> stringArrayValueTransform = (str, i1, i2) =>
{
    if (str == "Bacon") return "Hamster";

    if (i1 == 0) return "Bob";

    if (i1%5 == 0)
        return (i2%2 == 0)
            ? "OriginalGriff"
            : "Sheep";

    return "Vote this up :)";
};

// Transform the Array;
Transform2DArrayValues(ref strAry2, stringArrayValueTransform);

// Obviously a silly example:

//Bob,	Bob,	Bob,	Bob,	Bob,	Bob,	
//Vote #5 :),	Vote #5 :),	Hamster,	Vote #5 :),	Vote #5 :),	Vote #5 :),	
//Vote #5 :),	Vote #5 :),	Vote #5 :),	Vote #5 :),	Vote #5 :),	Vote #5 :),	
//Vote #5 :),	Vote #5 :),	Vote #5 :),	Vote #5 :),	Hamster,	Vote #5 :),	
//Vote #5 :),	Vote #5 :),	Vote #5 :),	Vote #5 :),	Vote #5 :),	Vote #5 :),	
//OriginalGriff,	Sheep,	OriginalGriff,	Sheep,	OriginalGriff,	Sheep,
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900