@@ -352,39 +352,68 @@ c =
352352 [4.0, 9.0]], shape=[6, 2], strides=[2, 1], layout=C (0x1), const ndim=2
353353```
354354
355- ### Stacking together different arrays
355+ ### Stacking/concatenating together different arrays
356+
357+ The ` stack! ` and ` concatenate! ` macros are helpful for stacking/concatenating
358+ arrays. The ` stack! ` macro stacks arrays along a new axis, while the
359+ ` concatenate! ` macro concatenates arrays along an existing axis:
356360
357- Macro ` stack! ` is helpful for stacking arrays:
358361``` rust
359362use ndarray :: prelude :: * ;
360- use ndarray :: {Array , Axis , stack };
363+ use ndarray :: {concatenate, stack, Axis };
361364
362365fn main () {
363-
364366 let a = array! [
365- [9 . , 7 . ],
366- [5 . , 2 . ]];
367-
367+ [3 . , 7 . , 8 . ],
368+ [5 . , 2 . , 4 . ],
369+ ];
370+
368371 let b = array! [
369- [1 . , 9 . ],
370- [5 . , 1 . ]];
371-
372- println! (" a vstack b = \ n {:?}\ n" , stack! [Axis (0 ), a , b ]);
373-
374- println! (" a hstack b = \ n {:?}\ n" , stack! [Axis (1 ), a , b ]);
372+ [1 . , 9 . , 0 . ],
373+ [5 . , 4 . , 1 . ],
374+ ];
375+
376+ println! (" stack, axis 0:\ n {:?}\ n" , stack! [Axis (0 ), a , b ]);
377+ println! (" stack, axis 1:\ n {:?}\ n" , stack! [Axis (1 ), a , b ]);
378+ println! (" stack, axis 2:\ n {:?}\ n" , stack! [Axis (2 ), a , b ]);
379+ println! (" concatenate, axis 0:\ n {:?}\ n" , concatenate! [Axis (0 ), a , b ]);
380+ println! (" concatenate, axis 1:\ n {:?}\ n" , concatenate! [Axis (1 ), a , b ]);
375381}
376382```
377383The output is:
378384```
379- a vstack b =
380- [[9.0, 7.0],
381- [5.0, 2.0],
382- [1.0, 9.0],
383- [5.0, 1.0]], shape=[4, 2], strides=[2, 1], layout=C (0x1), const ndim=2
385+ stack, axis 0:
386+ [[[3.0, 7.0, 8.0],
387+ [5.0, 2.0, 4.0]],
388+
389+ [[1.0, 9.0, 0.0],
390+ [5.0, 4.0, 1.0]]], shape=[2, 2, 3], strides=[6, 3, 1], layout=Cc (0x5), const ndim=3
391+
392+ stack, axis 1:
393+ [[[3.0, 7.0, 8.0],
394+ [1.0, 9.0, 0.0]],
395+
396+ [[5.0, 2.0, 4.0],
397+ [5.0, 4.0, 1.0]]], shape=[2, 2, 3], strides=[3, 6, 1], layout=c (0x4), const ndim=3
398+
399+ stack, axis 2:
400+ [[[3.0, 1.0],
401+ [7.0, 9.0],
402+ [8.0, 0.0]],
403+
404+ [[5.0, 5.0],
405+ [2.0, 4.0],
406+ [4.0, 1.0]]], shape=[2, 3, 2], strides=[1, 2, 6], layout=Ff (0xa), const ndim=3
407+
408+ concatenate, axis 0:
409+ [[3.0, 7.0, 8.0],
410+ [5.0, 2.0, 4.0],
411+ [1.0, 9.0, 0.0],
412+ [5.0, 4.0, 1.0]], shape=[4, 3], strides=[3, 1], layout=Cc (0x5), const ndim=2
384413
385- a hstack b =
386- [[9 .0, 7.0, 1.0, 9.0],
387- [5.0, 2.0, 5.0, 1.0]], shape=[2, 4 ], strides=[4, 1 ], layout=C (0x1 ), const ndim=2
414+ concatenate, axis 1:
415+ [[3 .0, 7.0, 8.0, 1.0, 9.0, 0 .0],
416+ [5.0, 2.0, 4.0, 5.0, 4.0, 1.0]], shape=[2, 6 ], strides=[1, 2 ], layout=Ff (0xa ), const ndim=2
388417```
389418
390419### Splitting one array into several smaller ones
0 commit comments