MCPcopy Create free account
hub / github.com/daniel-e/rustml / insert_column

Method insert_column

src/matrix.rs:717–738  ·  view source on GitHub ↗

Inserts a column before the specified column (indexing starts at zero). If the matrix is empty a new matrix `n x 1` matrix is returned where `n` is the number of elements in `v`. If `pos` is greater or equal to the number columns the vector is appended as a new column. If the matrix is not empty and `v.len() != self.rows()` a `None` is returned.

(&self, pos: usize, v: &[T])

Source from the content-addressed store, hash-verified

715 /// If the matrix is not empty and `v.len() != self.rows()` a
716 /// `None` is returned.
717 pub fn insert_column(&self, pos: usize, v: &[T]) -> Option<Matrix<T>> {
718
719 if self.empty() {
720 return Matrix::from_vec(v.to_vec(), v.len(), 1);
721 }
722
723 if v.len() != self.rows() {
724 return None;
725 }
726
727 let mut m = Matrix::<T>::new();
728 for (i, r) in self.row_iter().enumerate() {
729 let mut q = r.to_vec();
730 if pos < q.len() {
731 q.insert(pos, v[i].clone());
732 } else {
733 q.push(v[i].clone());
734 }
735 m.add_row(&q);
736 }
737 Some(m)
738 }
739
740 /// Returns a copy of the column at the specified index.
741 pub fn column(&self, pos: usize) -> Option<Vec<T>> {

Callers 2

from_col_vectorsMethod · 0.80
design_matrixMethod · 0.80

Calls 6

emptyMethod · 0.80
rowsMethod · 0.80
row_iterMethod · 0.80
add_rowMethod · 0.80
to_vecMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected