Skip to content

Commit 4bac2c1

Browse files
Added examples for indexing 2d arrays (#818)
* Added examples for indexing 2d arrays
1 parent f998a62 commit 4bac2c1

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

src/impl_2d.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ where
1717
/// Return an array view of row `index`.
1818
///
1919
/// **Panics** if `index` is out of bounds.
20+
///
21+
/// ```
22+
/// use ndarray::array;
23+
/// let array = array![[1., 2.], [3., 4.]];
24+
/// assert_eq!(array.row(0), array![1., 2.]);
25+
/// ```
2026
pub fn row(&self, index: Ix) -> ArrayView1<'_, A>
2127
where
2228
S: Data,
@@ -27,6 +33,13 @@ where
2733
/// Return a mutable array view of row `index`.
2834
///
2935
/// **Panics** if `index` is out of bounds.
36+
///
37+
/// ```
38+
/// use ndarray::array;
39+
/// let mut array = array![[1., 2.], [3., 4.]];
40+
/// array.row_mut(0)[1] = 5.;
41+
/// assert_eq!(array, array![[1., 5.], [3., 4.]]);
42+
/// ```
3043
pub fn row_mut(&mut self, index: Ix) -> ArrayViewMut1<'_, A>
3144
where
3245
S: DataMut,
@@ -35,6 +48,12 @@ where
3548
}
3649

3750
/// Return the number of rows (length of `Axis(0)`) in the two-dimensional array.
51+
///
52+
/// ```
53+
/// use ndarray::array;
54+
/// let array = array![[1., 2.], [3., 4.]];
55+
/// assert_eq!(array.nrows(), 2usize);
56+
/// ```
3857
pub fn nrows(&self) -> usize {
3958
self.len_of(Axis(0))
4059
}
@@ -48,6 +67,12 @@ where
4867
/// Return an array view of column `index`.
4968
///
5069
/// **Panics** if `index` is out of bounds.
70+
///
71+
/// ```
72+
/// use ndarray::array;
73+
/// let array = array![[1., 2.], [3., 4.]];
74+
/// assert_eq!(array.column(0), array![1., 3.]);
75+
/// ```
5176
pub fn column(&self, index: Ix) -> ArrayView1<'_, A>
5277
where
5378
S: Data,
@@ -58,6 +83,13 @@ where
5883
/// Return a mutable array view of column `index`.
5984
///
6085
/// **Panics** if `index` is out of bounds.
86+
///
87+
/// ```
88+
/// use ndarray::array;
89+
/// let mut array = array![[1., 2.], [3., 4.]];
90+
/// array.column_mut(0)[1] = 5.;
91+
/// assert_eq!(array, array![[1., 2.], [5., 4.]]);
92+
/// ```
6193
pub fn column_mut(&mut self, index: Ix) -> ArrayViewMut1<'_, A>
6294
where
6395
S: DataMut,
@@ -66,6 +98,12 @@ where
6698
}
6799

68100
/// Return the number of columns (length of `Axis(1)`) in the two-dimensional array.
101+
///
102+
/// ```
103+
/// use ndarray::array;
104+
/// let array = array![[1., 2.], [3., 4.]];
105+
/// assert_eq!(array.ncols(), 2usize);
106+
/// ```
69107
pub fn ncols(&self) -> usize {
70108
self.len_of(Axis(1))
71109
}
@@ -77,6 +115,20 @@ where
77115
}
78116

79117
/// Return true if the array is square, false otherwise.
118+
///
119+
/// # Examples
120+
/// Sqaure:
121+
/// ```
122+
/// use ndarray::array;
123+
/// let array = array![[1., 2.], [3., 4.]];
124+
/// assert!(array.is_square());
125+
/// ```
126+
/// Not sqaure:
127+
/// ```
128+
/// use ndarray::array;
129+
/// let array = array![[1., 2., 5.], [3., 4., 6.]];
130+
/// assert!(!array.is_square());
131+
/// ```
80132
pub fn is_square(&self) -> bool {
81133
self.nrows() == self.ncols()
82134
}

0 commit comments

Comments
 (0)