ssd
ssd

Reputation: 2432

How to extract a row from an array in C#?

note: BJ Myers comment was useful and was the answer in fact. However, as it was a comment, I couldn't mark that as an answer but I've placed the corrected code (using his advice) at the end of this question.

Original question below continues:

This situation may look weird at first but here is what I intend to do:

Similar to the syntax in Python, instead of creating a multidimensional array (a 2-d array, to be exact), I want to create an array of arrays (a vector of vectors, in fact).

I'm aware that C# will not let me create pointers in safe code, but I'm still curious whether there is a safer way to accomplish this task w/o getting of the safe code limits.

So, I came up with the code below but couldn't figure out how to extract a specific row from the array (as shown between the comment lines).

Is it possible to pass the r'th row at once or do I need to create another temporary storage for r'th row and then pass that temporary vector through?

(System: Windows-10, VS-2013, C#)

using System;

public class Vector {
    public double[] data;

    public Vector(double[] data) {
        this.data = new double[data.GetLength(0)];
        this.data = data;
    }
}

public class Matrix {
    private int row, col;

    public Matrix(double[,] data) {
        this.row = data.GetLength(0);
        this.col = data.GetLength(1);
        Vector[] v = new Vector[this.row];

        for (int r = 0; r < this.row; r++) {
            // ****** this line below ******
            v[r] = new Vector(data[r,???]);
            // ****** how to extract the r'th row ******
        }
    }

    static void Main(string[] args) {
        double[,] data = { { 9.0, 8.0, 7.0 }, { 5.0, 6.0, 4.0 }, { 3.0, 2.0, 2.0 } };
        Matrix A = new Matrix(data);
        Console.ReadLine();
    }
}

The corrected code is below:

using System;

public class Vector {
    public double[] data;

    public Vector(double[] data) {
        this.data = new double[data.GetLength(0)];
        this.data = data;
        for (int i = 0; i < data.GetLength(0); i++) {
            Console.Write("{0: 0.000 }", this.data[i]);
        }
        Console.WriteLine();
    }
}

public class Matrix {
    private int row, col;

    public Matrix(double[][] data) {
        this.row = data.GetLength(0);
        this.col = data[0].GetLength(0);
        Vector[] v = new Vector[this.row];

        for (int r = 0; r < row; r++) {
            v[r] = new Vector(data[r]);
        }

        Console.WriteLine("rows: " + this.row.ToString());
        Console.WriteLine("cols: " + this.col.ToString());
    }

    static void Main(string[] args) {
        double[][] data = { new double[] { 9.0, 8.0, 7.0 }, 
                            new double[] { 5.0, 6.0, 4.0 }, 
                            new double[] { 3.0, 2.0, 2.0 } };
        Matrix A = new Matrix(data);
        Console.ReadLine();
    }
}

Upvotes: 2

Views: 589

Answers (1)

A. Abramov
A. Abramov

Reputation: 1865

Well, you want to make an array class and acess like one? Make an indexer. what is an indexer? - it's a way to make your class accessible like an array.

Look over the link for examples, I'll help you with your specific case.

public class Vector {

public double[] data;
public double this[int i]
{
    get
    {
        // This indexer is very simple, and just returns or sets
        // the corresponding element from the internal array.
        return data[i];
    }
    set
    {
        data[i] = value;
    }
}
public Vector(double[] data) {
    this.data = new double[data.GetLength(0)];
    this.data = data;
}
}

once it's defined like so, this is perfectly valid:

double elementArray = new double[data.GetLength(1)]; // declaring an array, the size of the second dimention of the data array.
for(int i =0; i<data.GetLength(1);i++)
{
 elementArray[i] = data[r,i]; // adding all the elements to the list
}
v[r] = new Vector(elementArray);

EDIT: BJ Myers' comment is right, this solution works perfectly for a jagged array too, but make sure that you declare it properly like he mentioned.

EDIT 2: Using a list is pointless here, changed the stracture to an array.

Upvotes: 1

Related Questions