【问题标题】:Matrix operations in an Octave .oct fileOctave .oct 文件中的矩阵运算
【发布时间】:2013-10-07 07:13:43
【问题描述】:

我想在 .oct 文件中编写简洁的代码,以对输入列向量/矩阵执行矩阵乘法。例如,该函数在内部创建一个包含 11 个元素的行向量 A,然后在循环中我想执行 A * B 其中 B 是输入向量向下的 11 个元素滑动窗口,将结果写入输出列向量/矩阵随着循环的进行。我使用http://wiki.octave.org/Tips_and_tricks 上的信息尝试了各种方法,但我无法编译任何东西 - 我得到的错误是:

错误:无法在赋值中将“矩阵”转换为“双精度”

我的代码的相关部分是:-

Matrix price = args(0).matrix_value () ; // a single column matrix
Matrix output = args(0).matrix_value () ;

// create intermediate calculation matrices
Matrix vec_11 (1,11) ;
Matrix price_11 (11,1) ;
Matrix result (1,1) ;

for ( int ii = -m ; ii < (m+1) ; ii ++ )
 {
 vec_11 is filled in this loop and then doesn't change  
 }

for ( octave_idx_type ii (50) ; ii < args(0).length () ; ii++ )
 {
 // extract rolling window and perform matrix multiplication
 result = vec_11 * price.extract(ii-10,0,ii,0) ;
 output(ii,1) = result ; // this writing to output matrix fails
 }

retval_list(0) = output ; 

我正在使用价格、vec_11、结果和输出的矩阵值,因为我正在尝试进行矩阵乘法,所以我不明白为什么我会将此转换为双重错误。如果我将价格和输出更改为 column_vector_value 并将 vec_11 更改为 RowVector,我仍然会收到相同的错误。看来

result = vec_11 * price.extract(ii-10,0,ii,0) ;

给出了 matrix_value 但我不能将它写入 matrix_value 或 column_vector_value 输出。

【问题讨论】:

  • 你确定这是你正在编译的代码吗?您提到的错误与您显示的代码不匹配。您能否向我们展示重现错误的确切内容和最小示例?顺便说一句,如果你只有一个列/行,你应该使用'RowVector'和'ColumnVector'类,而不是'Matrix'。
  • @carandraug 你是对的 - 我草率地复制和粘贴。已编辑问题以显示正确的编译错误,并添加了一些额外信息以响应您的评论。

标签: for-loop matrix octave


【解决方案1】:

a mailing list 的帮助下,我终于能够解决我的问题了:

output(ii) = result( 0 , 0 ) ; // assign from indexed result matrix to output

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-16
    • 2022-01-01
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多