/ concept-collection / jupyterlite-numbl-kernel
Sign in
concept-collection / jupyterlite-numbl-kernel
jupyterlite-numbl-kernel / demo / content / 04-control-flow-and-functions.ipynb
105 lines · 2.8 KBCodeBlameHistory
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "id": "cf-0",
6 "metadata": {},
7 "source": "# Control flow and functions\n\nBranches, loops, anonymous functions, and named functions kept in `.m`\nfiles alongside the notebook."
8 },
9 {
10 "cell_type": "markdown",
11 "id": "cf-1",
12 "metadata": {},
13 "source": "## if / elseif / else"
14 },
15 {
16 "cell_type": "code",
17 "id": "cf-2",
18 "metadata": {},
19 "execution_count": null,
20 "outputs": [],
21 "source": "x = 7;\nif mod(x, 2) == 0\n disp('even')\nelseif x > 5\n disp('odd and > 5')\nelse\n disp('odd and <= 5')\nend"
22 },
23 {
24 "cell_type": "markdown",
25 "id": "cf-3",
26 "metadata": {},
27 "source": "## for and while loops"
28 },
29 {
30 "cell_type": "code",
31 "id": "cf-4",
32 "metadata": {},
33 "execution_count": null,
34 "outputs": [],
35 "source": "total = 0;\nfor k = 1:10\n total = total + k;\nend\ntotal"
36 },
37 {
38 "cell_type": "code",
39 "id": "cf-5",
40 "metadata": {},
41 "execution_count": null,
42 "outputs": [],
43 "source": "n = 0; p = 1;\nwhile p < 1000\n n = n + 1;\n p = p * 2;\nend\nfprintf('2^%d = %d is the first power of two >= 1000\\n', n, p);"
44 },
45 {
46 "cell_type": "markdown",
47 "id": "cf-6",
48 "metadata": {},
49 "source": "## switch"
50 },
51 {
52 "cell_type": "code",
53 "id": "cf-7",
54 "metadata": {},
55 "execution_count": null,
56 "outputs": [],
57 "source": "grade = 'B';\nswitch grade\n case 'A'\n disp('excellent')\n case {'B', 'C'}\n disp('good')\n otherwise\n disp('try again')\nend"
58 },
59 {
60 "cell_type": "markdown",
61 "id": "cf-8",
62 "metadata": {},
63 "source": "## Anonymous functions, arrayfun, and cellfun"
64 },
65 {
66 "cell_type": "code",
67 "id": "cf-9",
68 "metadata": {},
69 "execution_count": null,
70 "outputs": [],
71 "source": "sq = @(x) x.^2;\nsq(1:5)\narrayfun(@(k) k^2, 1:5)\ncellfun(@numel, {'a', 'bb', 'ccc'})"
72 },
73 {
74 "cell_type": "markdown",
75 "id": "cf-10",
76 "metadata": {},
9ff4e5bRestructure demo into a full guided tour with OOP and namespacesJeremy Magland 77 "source": "## Named functions in `.m` files\n\nCells work like the MATLAB console, so named functions live in `.m` files\nnext to the notebook. This one ships with [fib.m](./fib.m) and\n[minmax.m](./minmax.m); the kernel syncs them into the session so you can\ncall them from cells (and edit them in the file browser)."
79 {
80 "cell_type": "code",
81 "id": "cf-11",
82 "metadata": {},
83 "execution_count": null,
84 "outputs": [],
85 "source": "fibs = arrayfun(@fib, 1:10)\n[lo, hi] = minmax([3 -1 7 2])"
86 }
87 ],
88 "metadata": {
89 "kernelspec": {
90 "display_name": "numbl (MATLAB syntax)",
91 "language": "numbl",
92 "name": "numbl"
93 },
94 "language_info": {
95 "codemirror_mode": "octave",
96 "file_extension": ".m",
97 "mimetype": "text/x-octave",
98 "name": "numbl",
99 "nbconvert_exporter": "script",
100 "pygments_lexer": "matlab"
101 }
102 },
103 "nbformat": 4,
104 "nbformat_minor": 5
moveopenescclose