Skip to content

Commit 5d30944

Browse files
committed
doc: Elaborate nrows/ncols docs
Use a 3 x 2 array and show other ways of accessing axis lengths. Fixes #1120
1 parent 075199d commit 5d30944

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/impl_2d.rs

+26-6
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,19 @@ where
5050
/// Return the number of rows (length of `Axis(0)`) in the two-dimensional array.
5151
///
5252
/// ```
53-
/// use ndarray::array;
54-
/// let array = array![[1., 2.], [3., 4.]];
55-
/// assert_eq!(array.nrows(), 2usize);
53+
/// use ndarray::{array, Axis};
54+
///
55+
/// let array = array![[1., 2.],
56+
/// [3., 4.],
57+
/// [5., 6.]];
58+
/// assert_eq!(array.nrows(), 3);
59+
///
60+
/// // equivalent ways of getting the dimensions
61+
/// // get nrows, ncols by using dim:
62+
/// let (m, n) = array.dim();
63+
/// assert_eq!(m, array.nrows());
64+
/// // get length of any particular axis with .len_of()
65+
/// assert_eq!(m, array.len_of(Axis(0)));
5666
/// ```
5767
pub fn nrows(&self) -> usize {
5868
self.len_of(Axis(0))
@@ -94,9 +104,19 @@ where
94104
/// Return the number of columns (length of `Axis(1)`) in the two-dimensional array.
95105
///
96106
/// ```
97-
/// use ndarray::array;
98-
/// let array = array![[1., 2.], [3., 4.]];
99-
/// assert_eq!(array.ncols(), 2usize);
107+
/// use ndarray::{array, Axis};
108+
///
109+
/// let array = array![[1., 2.],
110+
/// [3., 4.],
111+
/// [5., 6.]];
112+
/// assert_eq!(array.ncols(), 2);
113+
///
114+
/// // equivalent ways of getting the dimensions
115+
/// // get nrows, ncols by using dim:
116+
/// let (m, n) = array.dim();
117+
/// assert_eq!(n, array.ncols());
118+
/// // get length of any particular axis with .len_of()
119+
/// assert_eq!(n, array.len_of(Axis(1)));
100120
/// ```
101121
pub fn ncols(&self) -> usize {
102122
self.len_of(Axis(1))

0 commit comments

Comments
 (0)