babelproofreader
babelproofreader

Reputation: 552

Write to a matrix in .oct file without looping?

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?

Upvotes: 1

Views: 342

Answers (1)

carandraug
carandraug

Reputation: 13081

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.

Upvotes: 3

Related Questions