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])
| 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>> { |