fill 3-dimensional array(jagged array) with point data - c#

I do have a 3-dimensional matrix
private int[][][] Matrix
but I dont know how to fill this.
the first dimension is for my slices of a picture
the second for my x-values of one slice ad the 3rd slice for my y-values.
so das anybody know how to fill this arrays with some data for testing?
thanks

You can do something like this:
Matrix = new int[5][][]; // 5 slices
Matrix[0] = new int[3][]; // 3 x values for the first slice
Matrix[0][0] = new int[2]; // 2 y values for the first x value in the first slice
But I don't think that you should use something like this. It is very error prone.
I suggest something like this:
class Slice
{
public IList<XValue> XValues {get; set; }
}
class XValue
{
public IList<YValue> YValues {get; set; }
}
class YValue
{
// ...
}
var slices = new List<Slice>();

You can use array literals to create arrays of arrays of arrays:
private int[][][] Matrix = {
{
{1,2,3},
{4,5}
},
{
{1,2},
{3},
{4,5,6,7,8},
{9,10}
},
{
{1,2,3}
}
};
Note that this is a jagged array, so different subarrays can have different number of items. If you want a three dimensional matrix you might want the three dimensional array int[,,] instead of nested one dimensional arrays.

Related

Syntax to create a double[,] in C#

This seems extremely simple, yet I can't seem to find applicable documentation anywhere. In C#, how do you create a 'double[,]'? Specifically, the data I'm trying to represent is something like this:
[0,0] = 0
[0,1] = 1
[1,0] = 1
[1,1] = 2
I have tried [[0,1],[1,2]] and the equivalent with {{}{}} and {[][]} and various other things, but cannot seem to figure out the syntax. It seems that a simple [0,1] alone is a 'double[,]' but I would like to find a way to represent the above data (more than just 2 numbers).
What am I missing? If anyone can point me to some simple documentation, that would be great.
See Array initializers:
For a multi-dimensional array, the array initializer must have as many levels of nesting as there are dimensions in the array. The outermost nesting level corresponds to the leftmost dimension and the innermost nesting level corresponds to the rightmost dimension. The length of each dimension of the array is determined by the number of elements at the corresponding nesting level in the array initializer. For each nested array initializer, the number of elements must be the same as the other array initializers at the same level.
In our case:
double[,] a = { { 0, 1 }, { 1, 2 } };
A multi-dimensional double array:
Double[,] newdouble = new Double[2,2];
or
Double[,] newdouble = { { 0, 0 }, { 1, 1 } };
In order to create a two dimensional array that you can assign to, you are going to need to first allocate the correct size. In this case, you have 2 rows and 2 columns, so that will be a [2,2].
double[,] twod = new double[2,2];
Next you simply assign to it like this
twod[0,0] = 0;
twod[0,1] = 1;
twod[1,0] = 1;
twod[1,1] = 2;
And then work with it however you wish.
There is three ways to initialized your array:
double[,] twoDemn = { { 0 , 1 }, { 1 , 2 } };
or:
double[,] twoDemn = new double[,] { { 0 , 1 }, { 1 , 2 } };
or:
double[,] twoDemn = new double[2,2];
twoDemn[0,0] = 0;
twoDemn[0,1] = 1;
twoDemn[1,0] = 1;
twoDemn[1,1] = 2;

How convert a list to int[][]

I have a class piece where I define each piece shape.
myShape.Add(new piece
{
Height = 3,
Width = 2,
Name = "3x2 L TopRight",
Size = 4,
Shape = new int[][] {
new int[] { 1, 0 },
new int[] { 1, 0 },
new int[] { 1, 1 }
}
});
But I create those shape by hand, now I reading the pieces in real time, so I create something like
List<int[]> virtualRow = new List<int[]>();
virtualRow.Add(new int[] { 1, 0 });
virtualRow.Add(new int[] { 1, 0 });
virtualRow.Add(new int[] { 1, 1 });
So how can I create Shape using virtualRow ?
I try something like
Shape = new int[][] { virtualRow.ToArray() }
But say
Cannot implicitly convert type 'int[][]' to 'int[]'
virtualRow.ToArray() is already an array of array of int values. You don't need to create a new array of array of ints and add this to it.
All you need is:
Shape = virtualRow.ToArray(),
virtualRow is a List of integer arrays, so to get an array of integer arrays you simply write:
Shape = virtualRow.ToArray();
...the return type of List.ToArray() being T[] as required.
Your code is in error because it attempts to add an int[][] to Shape instead of creating Shape as an int[][].
You want to do the following:
Shape = virtualRow.ToArray();
Since virtualRow is already a list of arrays. The ToArray function creates an int[][] object for your virtualRow, and all you need to do is store it to shape. What you were trying to do was create a matrix, within which was the result of the ToArray function. This way you are just storing the result of the function which gives you what you want.

Cannot instantiate 2-D array with dynamic size

I have created a class with a 2d integer array as a member. What I want, is for that member to be dynamically allocated as an x by x matrix when I instantiate a truck instance through the constructor. Here is my code:
public class truck
{
public int[][] mat;
truck(int x)
{
mat = new int[x][x];
for(int i=0;i<x;i++)
for(int j=0;j<x;j++)
mat[i][j]=0;
}
}
It is giving me the following error:
Invalid rank specifier: expected ',' or ']'
on this line: mat = new int[x][x];
Why is it giving me this error?
You can't initialize a jagged array like that. You have to initialize the first dimension first, and then initialize the elements:
mat = new int[x][];
//now you can do this:
mat[0] = new int[x];
...
Maybe you want a multidimensional array instead?
public int[,] mat;
...
mat = new int[x,x];
By using [][] you create an array of arrays. It means that each element of the first dimension contains an array.
That is why you need to initialize x arrays instead of just one.
You can do that by:
for (int i=0; i<x; i++) mat[i] = new int[x];
If you need to create just a two dimensional matrix, you can use int[,] instead of int[][]. In this case you would be able to initialize it like mat = new int[x,x]

C# 3 dimensional array

I want to store ARRAY from 3 dimensional array into buildingCostIds, but it says I MUST have 3rd number.
public static int[, ,] buildingCost = { {{0,1,2},{5,5,5}}};
public static void addBuilding(int[] ids, int[] amounts, int buildingId)
{
int[] buildingCostIds = buildingCost[buildingId, 0, *];
}
*I need third number here, but I don't want it because it will extract just number, I want whole array!
PROBLEM SOLVED, solution:
public static Array extractArray(int dim1, int dim2)
{
int[] tempArray = { };
for (int number=0;number<=2; number++)
{
tempArray[number] = buildingCost[dim1, dim2, number];
}
return tempArray;
}
This might be simpler if you use an array-of-arrays, or jagged array [][][] like
int[][][] buildingCost = new int[2][][];
Then you can access buildingCost[buildingId, 0] which is an array of ints.
There is a related question here
EDIT
Be aware the "problem solved" you added to the question duplicates the data, so you may run out of memory if you keep doing this.
Consider using a List of Lists of Lists and lazy evaluating what you need.
Try This
int[, ,] array3D = new int[3,3,3]

shipping boxes sizes find largest side and smallest size

I am doing some shipping calculations. I need some help trying to figure this out.
Basically, I have a generic list of products with Length, Width, and Height properties.
I would like to EASILY look at the products and find the largest values of all three properties.
From here, I can do some math and figure out the box size based on the # of products.
My initial thought would be to make 3 arrays and find the max of each. Just wanted to see if there was a simpler or cooler way I didnt' know.
Sounds like an array of arrays. As you read each element (box) from your data source (SQL Server, XML, etc), create an 3-member array and insert the attributes in order of size. Then, add the three-member array to an array of arrays. You can then sort the array of arrays by the first, second or third member using LINQ or some other function.
Box1,2,2,3
Box2,5,10,1
Box3,8,4,7
Becomes:
{ {10,5,1}, {8,7,4}, {3,2,2} } // First
or
{ {8,7,4}, {10,5,1}, {3,2,2} } // Second
or
{ {8,7,4}, {3,2,2}, {10,5,1} } // Third
Then, you can sort the array by the first element, second element, etc.
You could easily build an array of arrays in a single statement using LINQ, but exactly how you would do so depends on the data source. Assuming you have an class named Box with three parameters, Length, Width and Height, and that you have created a strongly-typed collection containing instances of this class:
class BoxSorter {
public IEnumerable<Box> Boxes {
get;
private set;
}
class Box {
public double Height {
get;
set;
}
public double Width {
get;
set;
}
public double Length {
get;
set;
}
}
public void Initialize() {
this.Boxes = new List<Box>( new Box[] {
new Box() { Height = 2, Length = 2, Width = 3 },
new Box() { Height = 5, Length = 10, Width = 1 },
new Box() { Height = 8, Length = 4, Width = 7 }
} );
}
public void Sort() {
var l_arrayOfArrays =
this.Boxes.Select(
// Create an array of the Height, Length and Width, then sort the array elements (largest to smallest)
b => new double[] { b.Height, b.Length, b.Width }.OrderByDescending( v => v ).ToArray()
);
var l_dimension1 =
l_arrayOfArrays.OrderByDescending(
// Sort the array of arrays by the first (and largest) dimension
a => a[0]
);
var l_dimension2 =
l_arrayOfArrays.OrderByDescending(
// Sort the array of arrays by the second (and middle) dimension
a => a[1]
);
var l_dimension3 =
l_arrayOfArrays.OrderByDescending(
// Sort the array of arrays by the third (and smallest) dimension
a => a[2]
);
}
}
What you probably need to do is to have a set of box sizes and THEN try packing them optimally in one or more boxes of that size.
This is a simple packer for the 2D case, you can extend this to 3D.
Your algorithm will sorta look like
foreach box in boxes (ordered by decreasing volume)
while there are unpacked items
if box has space
pack item
else
box = another box of the same size
Now you can decide what to do with unused space in the last box - either pick them all out and try a smaller box, or try packing all items in all size boxes and then pick the combination that results in least number of boxes.
You are really having a problem finding the min, max, and middle of three numbers?
It does not make sense to take the smallest of each column as if you had just two
4 x 4 x 4
8 x 8 x 2
You would incorrectly conclude the smallest is 4 x 4 x 2 and the largest is 8 x 8 x 4.
double[] dimensions;
dimensions = new double[] {8,7,7};
Array.Sort(dimensions);
System.Diagnostics.Debug.WriteLine(dimensions[0]);
System.Diagnostics.Debug.WriteLine(dimensions[1]);
System.Diagnostics.Debug.WriteLine(dimensions[2]);
dimensions = new double[] { 7, 9, 8 };
Array.Sort(dimensions);
System.Diagnostics.Debug.WriteLine(dimensions[0]);
System.Diagnostics.Debug.WriteLine(dimensions[1]);
System.Diagnostics.Debug.WriteLine(dimensions[2]);
P.S. I agree with anathonline that it is way more complex than just simple math if you want an optimal box size and how to pack the items.

Categories