forked from nipraxis/textbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comparing_arrays.Rmd
105 lines (87 loc) · 2.08 KB
/
comparing_arrays.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
---
jupyter:
jupytext:
text_representation:
extension: .Rmd
format_name: rmarkdown
format_version: '1.2'
jupytext_version: 1.11.5
---
$\newcommand{L}[1]{\| #1 \|}\newcommand{VL}[1]{\L{ \vec{#1} }}\newcommand{R}[1]{\operatorname{Re}\,(#1)}\newcommand{I}[1]{\operatorname{Im}\, (#1)}$
## Comparing arrays
A comparison between two arrays returns the *elementwise* result of the
comparison:
```{python}
import numpy as np
arr1 = np.array([[0, 1, 2],
[3, 4, 5]])
arr2 = np.array([[1, 1, 2],
[3, 4, 6]])
arr1 == arr2
```
Sometimes we want to know if two arrays are equal, in the sense that all the
elements of the two arrays are equal to each other. For this we use
`np.all`. `np.all` accepts an array as input, and returns True if all the
elements are non-zero .
```{python}
np.all([1, 2, 3])
```
Python assumes that `True == 1` and `False == 0` for this test of
non-zero:
```{python}
np.all([True, True, True])
```
```{python}
np.all([1, 2, 0])
np.all([True, False, True])
```
To ask whether all the elements in two arrays are equal, we can pass the
result of the element-wise comparison to `np.all`:
```{python}
np.all(arr1 == arr2)
```
```{python}
arr3 = arr1.copy()
np.all(arr1 == arr3)
```
Sometimes we want to know if any of the values in an array are non-zero
. Enter `np.any`:
```{python}
np.any([False, False, False])
```
```{python}
np.any([False, False, True])
```
```{python}
np.any(arr1 == arr2)
```
```{python}
np.any(arr1 != arr3)
```
<!-- vim:ft=rst -->
<!-- Course -->
<!-- BIC -->
<!-- Python distributions -->
<!-- Version control -->
<!-- Editors -->
<!-- Python and common libraries -->
<!-- IPython -->
<!-- Virtualenv and helpers -->
<!-- Pypi and packaging -->
<!-- Mac development -->
<!-- Windows development -->
<!-- Nipy and friends -->
<!-- FMRI datasets -->
<!-- Languages -->
<!-- Imaging software -->
<!-- Installation -->
<!-- Tutorials -->
<!-- MB tutorials -->
<!-- Ideas -->
<!-- Psych-214 -->
<!-- People -->
<!-- Licenses -->
<!-- Neuroimaging stuff -->
<!-- OpenFMRI projects -->
<!-- Unix -->
<!-- Substitutions -->