1
votes

Comment obtenir la dimension d'une matrice dans go-lang?

Voici le morceau de code:

package main
import (
 "fmt"
 "gonum.org/v1/gonum/mat"
)
func main() {
  // Matrix and Vector

  // Initialize a Matrix A
  row1 := []float64{1,2,3}
  row2 := []float64{4,5,6}
  row3 := []float64{7,8,9}
  row4 := []float64{10,11,12}

  A := mat.NewDense(4,3,nil)
  A.SetRow(0, row1)
  A.SetRow(1, row2)
  A.SetRow(2, row3)
  A.SetRow(3, row4)

  fmt.Printf("A :\n%v\n\n", mat.Formatted(A, mat.Prefix(""), mat.Excerpt(0)))

  // Initialize a Vector v
  v := mat.NewDense(3,1, []float64{1,2,3})
  fmt.Printf("v :\n%v\n\n", mat.Formatted(v, mat.Prefix(""), mat.Excerpt(0)))

  //Get the dimension of the matrix A where m = rows and n = cols
  row, col := len(A)
  // row, col := size(A)
  fmt.Println("row: ", row)
  fmt.Println("col: ", col)

}

Erreur: argument A invalide (type * mat.Dense) pour len

Lorsque j'utilise size pour déterminer les dimensions de la matrice A . Ensuite, cela me donne une erreur undefined: size .

Comment puis-je obtenir les dimensions de la matrice A?


0 commentaires

3 Réponses :


3
votes

len est utilisé pour les types intégrés tels que tableau, tranche, etc.

À partir du document du package, vous devez utiliser Dims () pour accéder à la taille des lignes et la taille de la colonne

essayez godoc gonum.org/v1/gonum/mat et trouvez les sections suivantes:

func (m *Dense) Dims() (r, c int)
Dims returns the number of rows and columns in the matrix.


0 commentaires

3
votes

Vous pouvez utiliser la méthode Dims () pour obtenir le nombre de lignes et de colonnes dans une matrice.

Voir: https://godoc.org/gonum.org /v1/gonum/mat#Dense.Dims


0 commentaires

2
votes

tapis de paquet

A matrix:
⎡ 1   2   3⎤
⎢ 4   5   6⎥
⎢ 7   8   9⎥
⎣10  11  12⎦

A: rows:  4
A: cols:  3

func (* Dense) Dims p >

package main

import (
    "fmt"

    "gonum.org/v1/gonum/mat"
)

func main() {
    row1 := []float64{1, 2, 3}
    row2 := []float64{4, 5, 6}
    row3 := []float64{7, 8, 9}
    row4 := []float64{10, 11, 12}

    A := mat.NewDense(4, 3, nil)
    A.SetRow(0, row1)
    A.SetRow(1, row2)
    A.SetRow(2, row3)
    A.SetRow(3, row4)

    fmt.Printf("A matrix:\n%v\n\n", mat.Formatted(A, mat.Prefix(""), mat.Excerpt(0)))

    //Get the dimensions of the matrix A
    rows, cols := A.Dims()
    fmt.Println("A: rows: ", rows)
    fmt.Println("A: cols: ", cols)
}

Dims renvoie le nombre de lignes et de colonnes dans la matrice.


Par exemple,

func (m *Dense) Dims() (r, c int)

Sortie:

import "gonum.org/v1/gonum/mat" 

0 commentaires