如何为大小可变的2×2数组分配所有元素?

huangapple go评论120阅读模式
英文:

GO : how to Assign all elements in 2 by 2 array of variable size?

问题

我遇到了一个问题,即使用GO从文本文件中填充一个2D数组的矩阵。主要问题是创建一个2D数组,因为我需要计算数组的维度,而GO似乎不接受数组维度中的VAR:

nb_lines = 数组的行数
nb_col = 数组的列数

// 从文件中读取矩阵
whole_file,_ := ioutil.ReadFile("test2.txt")
// 将文件的每一行存入tab_whole_file
tab_whole_file := strings.Split(string(whole_file), "\n")
// 表的第一行
tab_first_line := strings.Split(tab_whole_file[0], "\t")
nb_col := len(tab_first_line)
nb_lines := len(tab_whole_file) - 1

// 在这一点上,我尝试构建一个数组来存储来自文本文件的矩阵值

var columns [nb_lines][nb_col]float64 // 不起作用
columns := make([][]float64, nb_lines, nb_col) // 不起作用
columns := make([nb_lines][nb_col]float64) // 不起作用
columns := [nb_lines][nb_col]float64{} // 不起作用
columns := [][]float64{} // 报错:运行时错误:索引超出范围

for i := 0; i < nb_lines ; i++ { // 对于文本文件中的每一行
line := strings.Split(tab_whole_file[0], "\t") // 分割一行以获取每个表的值
for j := 1; j < len(line) ; j++ {
columns[i][j], _ = strconv.ParseFloat(line[j], 64) // 将每个值赋给表columns[i][j]
}
}

英文:

I'm having problems filling a 2D array with a matrix from a text file using GO.

The main problem I have is to create a 2D array because I have to calculate the dimension of the array and GO does not seem to accept VAR in array dimension :

  1. nb_lines = number of line of the array
  2. nb_col = number of columns of the array
  3. // read matrix from file
  4. whole_file,_ := ioutil.ReadFile(&quot;test2.txt&quot;)
  5. // get each line of the file in tab_whole_file
  6. tab_whole_file := strings.Split(string(whole_file), &quot;\n&quot;)
  7. // first line of the table
  8. tab_first_line := strings.Split(tab_whole_file[0], &quot;\t&quot;)
  9. nb_col := len(tab_first_line)
  10. nb_lines := len(tab_whole_file) - 1
  11. // at this point I tried to build a array to contain the matrix values from the texte file
  12. var columns [nb_lines][nb_col]float64 // does not work
  13. columns := make([][]float64, nb_lines, nb_col) // does not work
  14. columns := make([nb_lines][nb_col]float64) // does not work
  15. columns := [nb_lines][nb_col]float64{} // does not work
  16. columns := [][]float64{} // panic: runtime error: index out of range
  17. for i := 0; i &lt; nb_lines ; i++ { // for each line of the table from text file
  18. line := strings.Split(tab_whole_file[0], &quot;\t&quot;) // split one line to get each table values
  19. for j := 1; j &lt; len(line) ; j++ {
  20. columns[i][j], _ = strconv.ParseFloat(line[j], 64) // assign each value to the table columns[i][j]
  21. }
  22. }

答案1

得分: 1

在Go语言中,使用[][]float64声明一个float64类型的矩阵。

你可以使用:=运算符来初始化矩阵并自动声明类型。

注意,上面的代码行不仅仅是一个简单的声明,它是用于创建一个新的空切片的语法。

  1. // 创建一个空的整型切片
  2. x := []int{}
  3. // 创建一个整型切片
  4. x := []int{1, 2, 3, 4}

使用[]float64{}的方式将定义和分配结合在一起。注意以下两行代码的区别:

  1. // 创建一个包含4个整数的切片
  2. x := []int{1, 2, 3, 4}
  3. // 创建一个包含4个整数的数组
  4. x := [4]int{1, 2, 3, 4}

切片可以调整大小,而数组的大小是固定的。

针对你的问题,这里有一个示例演示了如何创建一个float64类型的矩阵并向矩阵中添加新行。

  1. package main
  2. import "fmt"
  3. func main() {
  4. matrix := [][]float64{}
  5. matrix = append(matrix, []float64{1.0, 2.0, 3.0})
  6. matrix = append(matrix, []float64{1.1, 2.1, 3.1})
  7. fmt.Println(matrix)
  8. }

你可以从这个示例开始,然后更新你的脚本。

英文:

A matrix of float64 in Go is declared as [][]float64

  1. var matrix [][]float64

You can use the := operator to initialize the matrix and declare the type automatically

  1. matrix := [][]float64{}

Note that this the line above is not a simple declaration, is the syntax used to create a new empty slice

  1. // creates an empty slice of ints
  2. x := []int{}
  3. // creates a slice of ints
  4. x := []int{1, 2, 3, 4}

The use of []float64{} combines together the definition and the allocation. Note that the following two lines are different

  1. // creates a slice of 4 ints
  2. x := []int{1, 2, 3, 4}
  3. // creates an array of 4 ints
  4. x := [4]int{1, 2, 3, 4}

Slices can be resized, arrays have fixed size.

Back to your problem, here's an example that demonstrates how to create a matrix of float64 and append new rows to the matrix.

  1. package main
  2. import &quot;fmt&quot;
  3. func main() {
  4. matrix := [][]float64{}
  5. matrix = append(matrix, []float64{1.0, 2.0, 3.0})
  6. matrix = append(matrix, []float64{1.1, 2.1, 3.1})
  7. fmt.Println(matrix)
  8. }

You can start from this example and update your script.

答案2

得分: 1

例如,

  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "strconv"
  7. )
  8. func loadMatrix(filename string) ([][]float64, error) {
  9. var m [][]float64
  10. data, err := ioutil.ReadFile(filename)
  11. if err != nil {
  12. return nil, err
  13. }
  14. rows := bytes.Split(data, []byte{'\n'})
  15. for r := len(rows) - 1; r >= 0; r-- {
  16. if len(rows[r]) != 0 {
  17. break
  18. }
  19. rows = rows[:len(rows)-1]
  20. }
  21. m = make([][]float64, len(rows))
  22. nCols := 0
  23. for r, row := range rows {
  24. cols := bytes.Split(row, []byte{'\t'})
  25. if r == 0 {
  26. nCols = len(cols)
  27. }
  28. m[r] = make([]float64, nCols)
  29. for c, col := range cols {
  30. if c < nCols && len(col) > 0 {
  31. e, err := strconv.ParseFloat(string(col), 64)
  32. if err != nil {
  33. return nil, err
  34. }
  35. m[r][c] = e
  36. }
  37. }
  38. }
  39. return m, nil
  40. }
  41. func main() {
  42. filename := "matrix.tsv"
  43. m, err := loadMatrix(filename)
  44. if err != nil {
  45. fmt.Println(err)
  46. return
  47. }
  48. fmt.Println("Matrix:")
  49. fmt.Println(m)
  50. fmt.Println("\n按[行,列]:")
  51. for r := range m {
  52. for c := range m[0] {
  53. fmt.Printf("[%d,%d] %5v ", r, c, m[r][c])
  54. }
  55. fmt.Println()
  56. }
  57. fmt.Println("\n按[列,行]:")
  58. for c := range m[0] {
  59. for r := range m {
  60. fmt.Printf("[%d,%d] %5v ", c, r, m[r][c])
  61. }
  62. fmt.Println()
  63. }
  64. }

输出:

  1. $ cat matrix.tsv
  2. 3.14 1.59 2.7 1.8
  3. 42
  4. $ go run matrix.go
  5. Matrix:
  6. [[3.14 1.59 2.7 1.8] [42 0 0 0]]
  7. 按[行,列]:
  8. [0,0] 3.14 [0,1] 1.59 [0,2] 2.7 [0,3] 1.8
  9. [1,0] 42 [1,1] 0 [1,2] 0 [1,3] 0
  10. 按[列,行]:
  11. [0,0] 3.14 [0,1] 42
  12. [1,0] 1.59 [1,1] 0
  13. [2,0] 2.7 [2,1] 0
  14. [3,0] 1.8 [3,1] 0
  15. $
英文:

For example,

  1. package main
  2. import (
  3. &quot;bytes&quot;
  4. &quot;fmt&quot;
  5. &quot;io/ioutil&quot;
  6. &quot;strconv&quot;
  7. )
  8. func loadMatrix(filename string) ([][]float64, error) {
  9. var m [][]float64
  10. data, err := ioutil.ReadFile(filename)
  11. if err != nil {
  12. return nil, err
  13. }
  14. rows := bytes.Split(data, []byte{&#39;\n&#39;})
  15. for r := len(rows) - 1; r &gt;= 0; r-- {
  16. if len(rows[r]) != 0 {
  17. break
  18. }
  19. rows = rows[:len(rows)-1]
  20. }
  21. m = make([][]float64, len(rows))
  22. nCols := 0
  23. for r, row := range rows {
  24. cols := bytes.Split(row, []byte{&#39;\t&#39;})
  25. if r == 0 {
  26. nCols = len(cols)
  27. }
  28. m[r] = make([]float64, nCols)
  29. for c, col := range cols {
  30. if c &lt; nCols &amp;&amp; len(col) &gt; 0 {
  31. e, err := strconv.ParseFloat(string(col), 64)
  32. if err != nil {
  33. return nil, err
  34. }
  35. m[r][c] = e
  36. }
  37. }
  38. }
  39. return m, nil
  40. }
  41. func main() {
  42. filename := &quot;matrix.tsv&quot;
  43. m, err := loadMatrix(filename)
  44. if err != nil {
  45. fmt.Println(err)
  46. return
  47. }
  48. fmt.Println(&quot;Matrix:&quot;)
  49. fmt.Println(m)
  50. fmt.Println(&quot;\nBy [row,column]:&quot;)
  51. for r := range m {
  52. for c := range m[0] {
  53. fmt.Printf(&quot;[%d,%d] %5v &quot;, r, c, m[r][c])
  54. }
  55. fmt.Println()
  56. }
  57. fmt.Println(&quot;\nBy [column,row]:&quot;)
  58. for c := range m[0] {
  59. for r := range m {
  60. fmt.Printf(&quot;[%d,%d] %5v &quot;, c, r, m[r][c])
  61. }
  62. fmt.Println()
  63. }
  64. }

Output:

  1. $ cat matrix.tsv
  2. 3.14 1.59 2.7 1.8
  3. 42
  4. $ go run matrix.go
  5. Matrix:
  6. [[3.14 1.59 2.7 1.8] [42 0 0 0]]
  7. By [row,column]:
  8. [0,0] 3.14 [0,1] 1.59 [0,2] 2.7 [0,3] 1.8
  9. [1,0] 42 [1,1] 0 [1,2] 0 [1,3] 0
  10. By [column,row]:
  11. [0,0] 3.14 [0,1] 42
  12. [1,0] 1.59 [1,1] 0
  13. [2,0] 2.7 [2,1] 0
  14. [3,0] 1.8 [3,1] 0
  15. $

答案3

得分: 0

我对程序进行了一些修改,这是一个“几乎”工作的示例...不太优雅 如何为大小可变的2×2数组分配所有元素? 矩阵中仍然存在一个问题:矩阵的第一列对应文件的第一行,所以我无法访问列 如何为大小可变的2×2数组分配所有元素?

  1. func main() {
  2. matrix := [][]float64{} // 对应文本文件的矩阵
  3. // 打开文件。
  4. whole_file,_ := ioutil.ReadFile("test2.tsv")
  5. // 测量表格的大小
  6. tab_whole_file := strings.Split(string(whole_file), "\n")
  7. tab_first_line := strings.Split(tab_whole_file[0], "\t")
  8. nb_col := len(tab_first_line)
  9. nb_lines := len(tab_whole_file) - 1
  10. // 我不知道为什么这个数字比我预期的要长,所以我必须减去1
  11. for i := 0; i < nb_lines ; i++ {
  12. numbers := []float64{} // 临时数组
  13. line := strings.Split(tab_whole_file[i], "\t")
  14. for j := 1; j < nb_col ; j++ { // 1而不是0,因为我不想要行名的第一列,也无法将其存储在浮点数组中
  15. if nb, err := strconv.ParseFloat(line[j], 64); err == nil {
  16. numbers = append(numbers,nb) // 不太优雅/高效的临时数组
  17. }
  18. }
  19. matrix = append(matrix, numbers)
  20. }
  21. }
英文:

I did few modifications to my program and this is a "almost" working example... not very elegant 如何为大小可变的2×2数组分配所有元素? There is still a problem in the matrix : the 1st column of the matrix correspond to the 1st line of the file so I dont have access to columns 如何为大小可变的2×2数组分配所有元素?

  1. func main() {
  2. matrix := [][]float64{} // matrix corresponding to the text file
  3. // Open the file.
  4. whole_file,_ := ioutil.ReadFile(&quot;test2.tsv&quot;)
  5. // mesasure the size of the table
  6. tab_whole_file := strings.Split(string(whole_file), &quot;\n&quot;)
  7. tab_first_line := strings.Split(tab_whole_file[0], &quot;\t&quot;)
  8. nb_col := len(tab_first_line)
  9. nb_lines := len(tab_whole_file) - 1
  10. // I dont know why this number is longer than I expected so I have to substract 1
  11. for i := 0; i &lt; nb_lines ; i++ {
  12. numbers := []float64{} // temp array
  13. line := strings.Split(tab_whole_file[i], &quot;\t&quot;)
  14. for j := 1; j &lt; nb_col ; j++ { // 1 instead of 0 because I dont want the first column of row names and I cant store it in a float array
  15. if nb, err := strconv.ParseFloat(line[j], 64); err == nil {
  16. numbers = append(numbers,nb) // not very elegant/efficient temporary array
  17. }
  18. }
  19. matrix = append(matrix, numbers)
  20. }
  21. }

huangapple
  • 本文由 发表于 2016年3月13日 20:07:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/35970010.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定