Write to a matrix in .oct file without looping?

喜你入骨 提交于 2019-12-11 05:29:46

问题


In an Octave .oct file it is possible to extract a submatrix thus:

B = A.extract(a-1,c-1,b-1,d-1) ;

the equivalent of B = A(a:b,c:d) in Octave code, but is it possible to write to a subset of a matrix in a similar manner,

A(a-1,c-1,b-1,d-1) = B ; // some other smaller matrix 

or would I have to loop over the relevant rows/columns and write element by element?


回答1:


Assuming A is of class Array, you can use one of the following methods (see the documentation):

Array<T> & insert (const Array<T> &a, octave_idx_type r, octave_idx_type c)
Array<T> & insert (const Array<T> &a, const Array<octave_idx_type> &idx)

You only need to know the subscript index for the top left corner (or its equivalent for N dimensions). The following will insert the 2D matrix B into the 2D matrix A, at coordinates (a, c)

A.insert (B, a, c);

For more dimensions, you'll need to create an Array<octave_idx_type> with coordinates for that point.



来源:https://stackoverflow.com/questions/19273053/write-to-a-matrix-in-oct-file-without-looping

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!