/ concept-collection / jupyterlite-numbl-kernel
Sign in
concept-collection / jupyterlite-numbl-kernel
jupyterlite-numbl-kernel / demo / content / 03-matrices-and-indexing.ipynb
133 lines · 2.7 KBBlameHistoryRaw
1{
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "id": "mx-0",
6 "metadata": {},
7 "source": "# Matrices and indexing\n\nMatrices are the core numbl data type: construction, reshaping, and\nMATLAB-style indexing."
8 },
9 {
10 "cell_type": "markdown",
11 "id": "mx-1",
12 "metadata": {},
13 "source": "## Constructing matrices"
14 },
15 {
16 "cell_type": "code",
17 "id": "mx-2",
18 "metadata": {},
19 "execution_count": null,
20 "outputs": [],
21 "source": "Z = zeros(2, 3)\nO = ones(2)\nI = eye(3)\nM = magic(4)"
22 },
23 {
24 "cell_type": "markdown",
25 "id": "mx-3",
26 "metadata": {},
27 "source": "## Ranges with the colon operator"
28 },
29 {
30 "cell_type": "code",
31 "id": "mx-4",
32 "metadata": {},
33 "execution_count": null,
34 "outputs": [],
35 "source": "1:5\n0:0.25:1\n10:-2:0"
36 },
37 {
38 "cell_type": "markdown",
39 "id": "mx-5",
40 "metadata": {},
41 "source": "## Size and reshaping"
42 },
43 {
44 "cell_type": "code",
45 "id": "mx-6",
46 "metadata": {},
47 "execution_count": null,
48 "outputs": [],
49 "source": "A = reshape(1:12, 3, 4)\nsize(A)\nnumel(A)\nA'"
50 },
51 {
52 "cell_type": "markdown",
53 "id": "mx-7",
54 "metadata": {},
55 "source": "## Concatenation"
56 },
57 {
58 "cell_type": "code",
59 "id": "mx-8",
60 "metadata": {},
61 "execution_count": null,
62 "outputs": [],
63 "source": "[1 2; 3 4]\n[ones(2, 2), zeros(2, 2)]"
64 },
65 {
66 "cell_type": "markdown",
67 "id": "mx-9",
68 "metadata": {},
69 "source": "## Indexing: subscripts, ranges, and `end`"
70 },
71 {
72 "cell_type": "code",
73 "id": "mx-10",
74 "metadata": {},
75 "execution_count": null,
76 "outputs": [],
77 "source": "M = magic(4);\nM(2, 3)\nM(:, 1)'\nM(end, :)\nM(1:2, 3:4)"
78 },
79 {
80 "cell_type": "markdown",
81 "id": "mx-11",
82 "metadata": {},
83 "source": "## Linear and logical indexing"
84 },
85 {
86 "cell_type": "code",
87 "id": "mx-12",
88 "metadata": {},
89 "execution_count": null,
90 "outputs": [],
91 "source": "M = magic(4);\nM(5)\nM(M > 10)'"
92 },
93 {
94 "cell_type": "markdown",
95 "id": "mx-13",
96 "metadata": {},
97 "source": "## Assignment, growth, and deletion"
98 },
99 {
100 "cell_type": "code",
101 "id": "mx-14",
102 "metadata": {},
103 "execution_count": null,
104 "outputs": [],
105 "source": "x = 1:5;\nx(2) = 20;\nx(8) = 99;\nx"
106 },
107 {
108 "cell_type": "code",
109 "id": "mx-15",
110 "metadata": {},
111 "execution_count": null,
112 "outputs": [],
113 "source": "y = 1:6;\ny(2:3) = [];\ny"
114 }
115 ],
116 "metadata": {
117 "kernelspec": {
118 "display_name": "numbl (MATLAB syntax)",
119 "language": "numbl",
120 "name": "numbl"
121 },
122 "language_info": {
123 "codemirror_mode": "octave",
124 "file_extension": ".m",
125 "mimetype": "text/x-octave",
126 "name": "numbl",
127 "nbconvert_exporter": "script",
128 "pygments_lexer": "matlab"
129 }
130 },
131 "nbformat": 4,
132 "nbformat_minor": 5
moveopenescclose