# 问题内容:

I have a matrix and I want to return an array containing as elements the sum of each row elements of the matrix.

``````int [] sum;

for (var i = 0; i < m; i++)
{
for (var j = 0; j < result.Pages[i].Actual.Count; j++)
{
sum[i] += result.Pages[i].Actual[j];
}

}
``````

This is how I tried to do it but seems it is not working. Any ideas?

## 问题评论:

you can simply get the length of each array dimension and use that to determine how many times to loop. google.co.uk/…
– user3654571
48 mins ago
what is `Pages` ? a `List` ?

# 答案:

## 答案1:

Use `var m = a.GetLength(0);` to get number of rows, and `var n = a.GetLength(1);` to get number of columns.

## 答案2:

now that looks like a different story after you edit:

Actually the first problem would be a `NullreferenceException` because `int[]sum` is not initialized!

Anyway, so it seems that you have an array of arrays. In this case you would need the `Length` of the `Pages` array to save your results. The first loop iterates over it using `i` and will run until `result.Pages.Length`. For each `i` you have correctly implemented a second loop where you sum up the result.

``````int [] sum = new int[result.Pages.Length];

for (var i = 0; i < result.Pages.Length; i++)
{
for (var j = 0; j < result.Pages[i].Actual.Length; j++)
{
sum[i] += result.Pages[i].Actual[j];
}
}
``````

If you collections are `List`‘s then you need to use `Count` instead of `Length`

The Linq solution would look like this:

``````int [] sum = result.Pages.Select(x=>x.Sum()).ToArray();
``````

EDIT:

`double?` means that you have a nullable data type. This is different from the normal `double`. Furthermore the default value will be `null` That means that you need to change the initialize the value at position `i`.

``````double? [] sum = new double?[result.Pages.Length];

for (var i = 0; i < result.Pages.Length; i++)
{
sum[i] = 0;
for (var j = 0; j < result.Pages[i].Actual.Length; j++)
{
sum[i] += result.Pages[i].Actual[j];
}
}
``````

## 答案评论:

thanks for your answer. it seems clear now. I only have one issue, the data is in `double?` format or that the error I’m get. I changed sum from `int` to `double` but still now effect
– user3654571
39 mins ago
@user3654571 “or that the error I’m get. ” I don’t see any error… does it have an error message? you really should post the code that you are testing, otherwise we keep playing a guessing game
this is the code and the error: pastebin.com/W8bhe2tW
– user3654571
34 mins ago
@user3654571 `double?` means that you have a nullable data type. This is different from the normal `double`. Furthermore the default value will be `null` That means that you need to change the initialize the value at position `i`

## 答案3:

I expected this to become a pretty one line linq approach – sorry but at least it’s one line

``````int[,] items = { { 1, 2, 3 }, { 2, 3, 4 }, { 3, 4, 5 } };
int[] sum = Enumerable.Range(0, items.GetLength(0))
.Select(x => Enumerable.Range(0, items.GetLength(1))
.Sum(y => items[x,y])).ToArray();
//sum[] == {6,9,12}
``````

for-loop appraoch

``````int[,] items = { { 1, 2, 3 }, { 2, 3, 4 }, { 3, 4, 5 } };
int[] sum = new int[items.GetLength(0)];
for (int i = 0; i < items.GetLength(0); i++)
for (int j = 0; j < items.GetLength(1); j++)
sum[i] += items[i, j];

//sum[] == {6,9,12}
``````

## 原文地址：

https://stackoverflow.com/questions/47755761/save-the-sum-of-each-row-of-a-matrix-into-an-array

Tags:,