{ "cells": [ { "cell_type": "markdown", "id": "mx-0", "metadata": {}, "source": "# Matrices and indexing\n\nMatrices are the core numbl data type. This notebook covers construction,\nreshaping, and MATLAB-style indexing." }, { "cell_type": "markdown", "id": "mx-1", "metadata": {}, "source": "## Constructing matrices" }, { "cell_type": "code", "id": "mx-2", "metadata": {}, "execution_count": null, "outputs": [], "source": "Z = zeros(2, 3)\nO = ones(2)\nI = eye(3)\nM = magic(4)" }, { "cell_type": "markdown", "id": "mx-3", "metadata": {}, "source": "## Ranges with the colon operator" }, { "cell_type": "code", "id": "mx-4", "metadata": {}, "execution_count": null, "outputs": [], "source": "1:5\n0:0.25:1\n10:-2:0" }, { "cell_type": "markdown", "id": "mx-5", "metadata": {}, "source": "## Size and reshaping" }, { "cell_type": "code", "id": "mx-6", "metadata": {}, "execution_count": null, "outputs": [], "source": "A = reshape(1:12, 3, 4)\nsize(A)\nnumel(A)\nA'" }, { "cell_type": "markdown", "id": "mx-7", "metadata": {}, "source": "## Concatenation" }, { "cell_type": "code", "id": "mx-8", "metadata": {}, "execution_count": null, "outputs": [], "source": "[1 2; 3 4]\n[ones(2, 2), zeros(2, 2)]" }, { "cell_type": "markdown", "id": "mx-9", "metadata": {}, "source": "## Indexing: subscripts, ranges, and `end`" }, { "cell_type": "code", "id": "mx-10", "metadata": {}, "execution_count": null, "outputs": [], "source": "M = magic(4);\nM(2, 3)\nM(:, 1)'\nM(end, :)\nM(1:2, 3:4)" }, { "cell_type": "markdown", "id": "mx-11", "metadata": {}, "source": "## Linear and logical indexing" }, { "cell_type": "code", "id": "mx-12", "metadata": {}, "execution_count": null, "outputs": [], "source": "M = magic(4);\nM(5)\nM(M > 10)'" }, { "cell_type": "markdown", "id": "mx-13", "metadata": {}, "source": "## Assignment, growth, and deletion" }, { "cell_type": "code", "id": "mx-14", "metadata": {}, "execution_count": null, "outputs": [], "source": "x = 1:5;\nx(2) = 20;\nx(8) = 99;\nx" }, { "cell_type": "code", "id": "mx-15", "metadata": {}, "execution_count": null, "outputs": [], "source": "y = 1:6;\ny(2:3) = [];\ny" } ], "metadata": { "kernelspec": { "display_name": "numbl (MATLAB syntax)", "language": "numbl", "name": "numbl" }, "language_info": { "codemirror_mode": "octave", "file_extension": ".m", "mimetype": "text/x-octave", "name": "numbl", "nbconvert_exporter": "script", "pygments_lexer": "matlab" } }, "nbformat": 4, "nbformat_minor": 5 }