如何使用Cgo访问MATLAB数组中的值?

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

How do I access the values inside a MATLAB array using Cgo?

问题

使用MatLab C API和Go的Cgo包,我正在尝试在我的Go程序中读取一个24x3000000的矩阵内的mat文件。我能够成功读取矩阵的维度,但是如何访问每个单元格内的值呢?(最终目标是将该矩阵作为切片返回给我的Go程序。)

  1. var realDataPtr *C.double
  2. var field *C.mxArray
  3. fieldName := C.CString("data")
  4. field = C.mxGetField(pa, 0, fieldName)
  5. rowTotal := C.mxGetM(field) // 输出为24
  6. colTotal := C.mxGetN(field) // 输出为3000000
  7. // 获取矩阵中数据的指针
  8. realDataPtr = C.mxGetPr(field)
  9. // 打印矩阵中的每个元素
  10. for row := 0; row < int(rowTotal); row++ {
  11. for col := 0; col < int(colTotal); col++ {
  12. // 这是我卡住的地方
  13. }
  14. }

参考资料:这里是C语言的矩阵库API。

英文:

Using the MatLab C API and Go's Cgo package, I'm attempting to read a 24x3000000 matrix inside a mat-file in my Go program. I'm able to successfully read the dimensions of the matrix but how do I access the values inside each cell? (The goal is ultimately to return this matrix as a slice to my Go program.)

  1. var realDataPtr *C.double
  2. var field *C.mxArray
  3. fieldName := C.CString(&quot;data&quot;)
  4. field = C.mxGetField(pa, 0, fieldName)
  5. rowTotal := C.mxGetM(field) // outputs 24
  6. colTotal := C.mxGetN(field) // outputs 3000000
  7. // get pointer to data in matrix
  8. realDataPtr = C.mxGetPr(field)
  9. // Print every element in the matrix
  10. for row := 0; row &lt; int(rowTotal); row++ {
  11. for col := 0; col &lt; int(colTotal); col++ {
  12. // This is where I get stuck
  13. }
  14. }

For reference, here's the Matrix Library API for C

答案1

得分: 1

MATLAB中的矩阵数据以列优先顺序存储,这意味着数据的列按顺序存储在内存中(与C和类似的语言相反)。因此,如果你想在C中按顺序访问数据(很遗憾,我对Go语法不太熟悉),你可以这样做:

  1. for(col=0;col<colTotal;++col)
  2. {
  3. for(row=0;row<rowTotal;++row)
  4. {
  5. data = realDataPtr[col*rowTotal + row];
  6. }
  7. }
英文:

Matrix data in MATLAB are stored in column major order, which means that the columns of the data are stored in sequential order in memory (this is the opposite to C and similar languages). Thus, if you wanted to access the data sequentially in C (I'm not immediately familiar with Go syntax, unfortunately), you could do the following:

  1. for(col=0;col&lt;colTotal;++col)
  2. {
  3. for(row=0;row&lt;rowTotal;++row)
  4. {
  5. data = realDataPtr[col*rowTotal + row];
  6. }
  7. }

答案2

得分: 1

未经测试,因为我没有MatLab。例如,

  1. import (
  2. "fmt"
  3. "unsafe"
  4. )
  5. // 打印矩阵中的每个元素
  6. ptr := uintptr(unsafe.Pointer(realDataPtr))
  7. for col := 0; col < int(colTotal); col++ {
  8. for row := 0; row < int(rowTotal); row++ {
  9. // 这是我卡住的地方
  10. elem := *(*float64)(unsafe.Pointer(ptr))
  11. fmt.Println(elem)
  12. ptr += 8
  13. }
  14. }
英文:

Untested, since I don't have MatLab. For example,

  1. import (
  2. &quot;fmt&quot;
  3. &quot;unsafe&quot;
  4. )
  5. // Print every element in the matrix
  6. ptr := uintptr(unsafe.Pointer(realDataPtr))
  7. for col := 0; col &lt; int(colTotal); col++ {
  8. for row := 0; row &lt; int(rowTotal); row++ {
  9. // This is where I get stuck
  10. elem := *(*float64)(unsafe.Pointer(ptr))
  11. fmt.Println(elem)
  12. ptr += 8
  13. }
  14. }

huangapple
  • 本文由 发表于 2014年11月2日 09:37:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/26695158.html
匿名

发表评论

匿名网友

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

确定