Contents
- A double percentage sign without spaces creates a cell
- After a double percentage (and a space), the text is bolded
- We strongly recommend to start each file with a header
- 0 Pretend header: Lab 1 - from 0 to 0.01
- 1 Arithmetic operations - everything works as you would expect
- Variables - in MATLAB, we store values (usually numbers) in these
- 3 Functions - if variables are your matter, and forces are your operators, these are they
- 4 Vectors! Vectors are stacks of numbers
- 5 Matrices - stacks of vectors!
% Everything after a percentage sign is ignored by MATLAB % This is useful to make notes for yourself.
A double percentage sign without spaces creates a cell
%Use cells to break up your code into meaningful segments, e.g. % loading data, analyzing data, plotting data
After a double percentage (and a space), the text is bolded
We strongly recommend to start each file with a header
%What is this? (What does it do, assumptions, outputs) %Who did it? %How do I reach them? %When? (Versioning)
0 Pretend header: Lab 1 - from 0 to 0.01
%Philosophy: MATLAB as a graphing linear algebra calculator %You %09/08/2016
1 Arithmetic operations - everything works as you would expect
5 + 2 %Plus 5 - 2 %Minus 5 * 2 %Multiplication 5/2 %Division 5^2 %Exponentiation 5^0.5 %Square root %Order of operations: Also everything as expected (5 + 4/2)^2 %White space or - more neutrally - blank space: %Is ignored in MATLAB (not Python) 5 + 2 5 + 2 5 + 2 5 + 2
ans =
7
ans =
3
ans =
10
ans =
2.500000000000000
ans =
25
ans =
2.236067977499790
ans =
49
ans =
7
ans =
7
ans =
7
ans =
7
Variables - in MATLAB, we store values (usually numbers) in these
A = 5 B = 2.5 C = 2*B D = 5 + 2 % = equality sign looks suspiciously like testing for equality % but is actually an assignment operator. It takes whatever is on the % right side and overwrites the left side %Testing for equality: 2 equal signs: == A == A A == B A == C %How to access variables, say I want to know what is in D. %In case I forgot D %Just type the variable name %Variable names: What can you name variables? %Can be anything you want, BUT must start with a letter %cannot have a white space in it and ideally should not %collide with a pre-existing function. %What should you name them? Something descriptive %We recommend camelCase - every new word is capitalized for parsing %This is superior to underscores, which are ugly. numParticipants = 500; %More power %What is ans? %ans is a temporary variable that MATLAB creates whenever %you do not explicitly ask for a left side variable to %assign whatever you do to. It has to go somewhere %It is overwritten every time you make a new command. %So this is not for permanent storage, but can be useful %for iterative procedures. You *can* compute with it. 5 + 5 %ans will become 10 ans + 5 %ans will become 15 ans + ans %ans will become 30
A =
5
B =
2.500000000000000
C =
5
D =
7
ans =
1
ans =
0
ans =
1
D =
7
ans =
10
ans =
15
ans =
30
3 Functions - if variables are your matter, and forces are your operators, these are they
%Recall that we did square rooting with exponents 9^0.5 %Exponent way sqrt(9) %Functional - all arguments to functions are denoted by parentheses () abs(2) abs(-2) %help abs is the quick and dirty inline help %doc abs brings up the relevant entry in the MATLAB documentation %Finally, if you know you need help, but don't even know where to begin to %look, click on the function browser and search for keywords. %Functions can be concatenated, chained, nested sqrt(-3) %This will throw us on the complex plane sqrt(abs(3)) %Let's keep it real %Which function does MATLAB execute first? The innermost nested one rand(1) %Pull 1 random number from 0 to 1 out of a hat. A well mixed urn. %Every time you execute this, you'll get a new number, but if you all %share the same seed (or MATLAB license), the sequence will be the same! abs(-2) %2, as you would expect abs = abs(-4) %Say I want to store this as a constant %abs(-2) %Now commented out so that running this doesn't crash the script %We can fix our mistake by calling clear %As in clear abs %Unless we overwrite clear also, in which case we have to right-click and %delete it in the workspace itself. %clear all deletes all variables from the workspace %As does clear, since a couple of versions clear all which numParticipants %If you are unsure of whether it exists %The most common problem I see is by calling something "size" %We might as well talk about formatting vs. precision %Formatting controls how you see the output without changing the internal %representation %Precision changes how MATLAB represents a number internally. There is an %inherent tradeoff between resolution and memory demands. A = sqrt(7) %Assign square root of 7 to variable A format long %16 after-digit numbers A format short %4 after-digit numbers A format bank %2 after-digit numbers A format rat %rational approximation (as a fraction) A format long %back to long %NONE of these change how MATLAB represents the numbers internally %Double vs. single precision (MATLAB default: double) B = 2^127+1 %A big number - but not that big to be represented as a double. Double = 8 bytes = 64 binary digits. Switches. C = single(B) %Setting precision of C to single, with value B. single = 4 bytes = 32 binary digits or switches B = 2^128 %A number that is just 1 larger C = single(B) %But it can no longer be represented as single %Both single and double precision can accomodate numbers that are larger %than anything you are likely to encounter in neuroscience. However, some %precisions are rather limited, e.g. if you want to represent a number with %1 byte = 8 bits = 8 switches = 2^8 = 256. B = 256 C = uint8(B) %unsigned integer 8 bit - can represent 256 states from 0 to 255 %255 is the infinity of uint8, you can not represent numbers larger than %that. %Also, only integers - if the result is irrational (or not integral), it %simply rounds: uint8(sqrt(7)) %The benefit? Memory! %That's it for now about variables and how they are represented
ans =
3
ans =
3
ans =
2
ans =
2
ans =
0.000000000000000 + 1.732050807568877i
ans =
1.732050807568877
ans =
0.141886338627215
ans =
2
abs =
4
'numParticipants' not found.
A =
2.645751311064591
A =
2.645751311064591
A =
2.6458
A =
2.65
A =
2024/765
B =
1.701411834604692e+38
C =
1.7014118e+38
B =
3.402823669209385e+38
C =
Inf
B =
256
C =
255
ans =
3
4 Vectors! Vectors are stacks of numbers
%Row vector with 5 elements A = [1, 2, 3, 4, 2] %Sets of numbers to be assigned to a variable all at once are denoted by SQUARE brackets [] %The comma denotes horizontal concatenation of numbers (to create our row %vector), it is implied - if you don't put anything, MATLAB will assume a %comma %Column vector with 3 elements B = [3; 5; 4] %Semicolon HERE means vertical concatenation %Dimensionality can be assessed by looking at the workspace %OR by calling the size function size(A) size(B) %Convention: Rows are first, columns are second, sheets are third and %workbooks are 4th and so on. You can have as many dimensions as you want. %But a vector has 2 and one of them is of dimensionality 1. %It's a stack of numbers. %Now you know why you should never call any variable "size", no matter %how tempting it might be. %Converting row vectors to column vectors and vice versa %Transpose! transpose(B) B' %Vector operations: Most functions in MATLAB operate on vectors natively mean(A) mean(B) sum(A) sum(B) %We need to talk about the echo A = [1:1000] %This creates a vector from 1 to 1000. Colon means "from, to" B = [1:1000]; %Semicolon turns the echo off. It still executes the command, but you no longer see immediate inline output %The third function of the semicolon in addition to vertical concatenation %of numbers and echo off (echo is on by default) is concatentation of %inline commands C = [1; 2; 3]; D = sum(C); %All the semicolons used here have different meanings %As you see in C, some semicolons even serve multiple functions!
A =
1 2 3 4 2
B =
3
5
4
ans =
1 5
ans =
3 1
ans =
3 5 4
ans =
3 5 4
ans =
2.400000000000000
ans =
4
ans =
12
ans =
12
A =
Columns 1 through 6
1 2 3 4 5 6
Columns 7 through 12
7 8 9 10 11 12
Columns 13 through 18
13 14 15 16 17 18
Columns 19 through 24
19 20 21 22 23 24
Columns 25 through 30
25 26 27 28 29 30
Columns 31 through 36
31 32 33 34 35 36
Columns 37 through 42
37 38 39 40 41 42
Columns 43 through 48
43 44 45 46 47 48
Columns 49 through 54
49 50 51 52 53 54
Columns 55 through 60
55 56 57 58 59 60
Columns 61 through 66
61 62 63 64 65 66
Columns 67 through 72
67 68 69 70 71 72
Columns 73 through 78
73 74 75 76 77 78
Columns 79 through 84
79 80 81 82 83 84
Columns 85 through 90
85 86 87 88 89 90
Columns 91 through 96
91 92 93 94 95 96
Columns 97 through 102
97 98 99 100 101 102
Columns 103 through 108
103 104 105 106 107 108
Columns 109 through 114
109 110 111 112 113 114
Columns 115 through 120
115 116 117 118 119 120
Columns 121 through 126
121 122 123 124 125 126
Columns 127 through 132
127 128 129 130 131 132
Columns 133 through 138
133 134 135 136 137 138
Columns 139 through 144
139 140 141 142 143 144
Columns 145 through 150
145 146 147 148 149 150
Columns 151 through 156
151 152 153 154 155 156
Columns 157 through 162
157 158 159 160 161 162
Columns 163 through 168
163 164 165 166 167 168
Columns 169 through 174
169 170 171 172 173 174
Columns 175 through 180
175 176 177 178 179 180
Columns 181 through 186
181 182 183 184 185 186
Columns 187 through 192
187 188 189 190 191 192
Columns 193 through 198
193 194 195 196 197 198
Columns 199 through 204
199 200 201 202 203 204
Columns 205 through 210
205 206 207 208 209 210
Columns 211 through 216
211 212 213 214 215 216
Columns 217 through 222
217 218 219 220 221 222
Columns 223 through 228
223 224 225 226 227 228
Columns 229 through 234
229 230 231 232 233 234
Columns 235 through 240
235 236 237 238 239 240
Columns 241 through 246
241 242 243 244 245 246
Columns 247 through 252
247 248 249 250 251 252
Columns 253 through 258
253 254 255 256 257 258
Columns 259 through 264
259 260 261 262 263 264
Columns 265 through 270
265 266 267 268 269 270
Columns 271 through 276
271 272 273 274 275 276
Columns 277 through 282
277 278 279 280 281 282
Columns 283 through 288
283 284 285 286 287 288
Columns 289 through 294
289 290 291 292 293 294
Columns 295 through 300
295 296 297 298 299 300
Columns 301 through 306
301 302 303 304 305 306
Columns 307 through 312
307 308 309 310 311 312
Columns 313 through 318
313 314 315 316 317 318
Columns 319 through 324
319 320 321 322 323 324
Columns 325 through 330
325 326 327 328 329 330
Columns 331 through 336
331 332 333 334 335 336
Columns 337 through 342
337 338 339 340 341 342
Columns 343 through 348
343 344 345 346 347 348
Columns 349 through 354
349 350 351 352 353 354
Columns 355 through 360
355 356 357 358 359 360
Columns 361 through 366
361 362 363 364 365 366
Columns 367 through 372
367 368 369 370 371 372
Columns 373 through 378
373 374 375 376 377 378
Columns 379 through 384
379 380 381 382 383 384
Columns 385 through 390
385 386 387 388 389 390
Columns 391 through 396
391 392 393 394 395 396
Columns 397 through 402
397 398 399 400 401 402
Columns 403 through 408
403 404 405 406 407 408
Columns 409 through 414
409 410 411 412 413 414
Columns 415 through 420
415 416 417 418 419 420
Columns 421 through 426
421 422 423 424 425 426
Columns 427 through 432
427 428 429 430 431 432
Columns 433 through 438
433 434 435 436 437 438
Columns 439 through 444
439 440 441 442 443 444
Columns 445 through 450
445 446 447 448 449 450
Columns 451 through 456
451 452 453 454 455 456
Columns 457 through 462
457 458 459 460 461 462
Columns 463 through 468
463 464 465 466 467 468
Columns 469 through 474
469 470 471 472 473 474
Columns 475 through 480
475 476 477 478 479 480
Columns 481 through 486
481 482 483 484 485 486
Columns 487 through 492
487 488 489 490 491 492
Columns 493 through 498
493 494 495 496 497 498
Columns 499 through 504
499 500 501 502 503 504
Columns 505 through 510
505 506 507 508 509 510
Columns 511 through 516
511 512 513 514 515 516
Columns 517 through 522
517 518 519 520 521 522
Columns 523 through 528
523 524 525 526 527 528
Columns 529 through 534
529 530 531 532 533 534
Columns 535 through 540
535 536 537 538 539 540
Columns 541 through 546
541 542 543 544 545 546
Columns 547 through 552
547 548 549 550 551 552
Columns 553 through 558
553 554 555 556 557 558
Columns 559 through 564
559 560 561 562 563 564
Columns 565 through 570
565 566 567 568 569 570
Columns 571 through 576
571 572 573 574 575 576
Columns 577 through 582
577 578 579 580 581 582
Columns 583 through 588
583 584 585 586 587 588
Columns 589 through 594
589 590 591 592 593 594
Columns 595 through 600
595 596 597 598 599 600
Columns 601 through 606
601 602 603 604 605 606
Columns 607 through 612
607 608 609 610 611 612
Columns 613 through 618
613 614 615 616 617 618
Columns 619 through 624
619 620 621 622 623 624
Columns 625 through 630
625 626 627 628 629 630
Columns 631 through 636
631 632 633 634 635 636
Columns 637 through 642
637 638 639 640 641 642
Columns 643 through 648
643 644 645 646 647 648
Columns 649 through 654
649 650 651 652 653 654
Columns 655 through 660
655 656 657 658 659 660
Columns 661 through 666
661 662 663 664 665 666
Columns 667 through 672
667 668 669 670 671 672
Columns 673 through 678
673 674 675 676 677 678
Columns 679 through 684
679 680 681 682 683 684
Columns 685 through 690
685 686 687 688 689 690
Columns 691 through 696
691 692 693 694 695 696
Columns 697 through 702
697 698 699 700 701 702
Columns 703 through 708
703 704 705 706 707 708
Columns 709 through 714
709 710 711 712 713 714
Columns 715 through 720
715 716 717 718 719 720
Columns 721 through 726
721 722 723 724 725 726
Columns 727 through 732
727 728 729 730 731 732
Columns 733 through 738
733 734 735 736 737 738
Columns 739 through 744
739 740 741 742 743 744
Columns 745 through 750
745 746 747 748 749 750
Columns 751 through 756
751 752 753 754 755 756
Columns 757 through 762
757 758 759 760 761 762
Columns 763 through 768
763 764 765 766 767 768
Columns 769 through 774
769 770 771 772 773 774
Columns 775 through 780
775 776 777 778 779 780
Columns 781 through 786
781 782 783 784 785 786
Columns 787 through 792
787 788 789 790 791 792
Columns 793 through 798
793 794 795 796 797 798
Columns 799 through 804
799 800 801 802 803 804
Columns 805 through 810
805 806 807 808 809 810
Columns 811 through 816
811 812 813 814 815 816
Columns 817 through 822
817 818 819 820 821 822
Columns 823 through 828
823 824 825 826 827 828
Columns 829 through 834
829 830 831 832 833 834
Columns 835 through 840
835 836 837 838 839 840
Columns 841 through 846
841 842 843 844 845 846
Columns 847 through 852
847 848 849 850 851 852
Columns 853 through 858
853 854 855 856 857 858
Columns 859 through 864
859 860 861 862 863 864
Columns 865 through 870
865 866 867 868 869 870
Columns 871 through 876
871 872 873 874 875 876
Columns 877 through 882
877 878 879 880 881 882
Columns 883 through 888
883 884 885 886 887 888
Columns 889 through 894
889 890 891 892 893 894
Columns 895 through 900
895 896 897 898 899 900
Columns 901 through 906
901 902 903 904 905 906
Columns 907 through 912
907 908 909 910 911 912
Columns 913 through 918
913 914 915 916 917 918
Columns 919 through 924
919 920 921 922 923 924
Columns 925 through 930
925 926 927 928 929 930
Columns 931 through 936
931 932 933 934 935 936
Columns 937 through 942
937 938 939 940 941 942
Columns 943 through 948
943 944 945 946 947 948
Columns 949 through 954
949 950 951 952 953 954
Columns 955 through 960
955 956 957 958 959 960
Columns 961 through 966
961 962 963 964 965 966
Columns 967 through 972
967 968 969 970 971 972
Columns 973 through 978
973 974 975 976 977 978
Columns 979 through 984
979 980 981 982 983 984
Columns 985 through 990
985 986 987 988 989 990
Columns 991 through 996
991 992 993 994 995 996
Columns 997 through 1000
997 998 999 1000
5 Matrices - stacks of vectors!
%Matrix creation A = [1 2 3;4 5 6;7 8 9] %Stacking row vectors size(A) %Dimensionality numel(A) %Number of elements %Accessing elements of matrix elements and reassinging them %I want to access the 2nd row A(2,:) %Colon without arguments means "all" %I want to access the last 2 elements of the 3rd row A(3,2:3) A(3,2:end) %As you see, there are many ways to do this %I want to access the first column A(:,1) %I want to access the element in the 3rd column and 3rd row A(3,3) %I want to reassign the number 55 to that element A(3,3) = 55; %I want to delete the 3rd column A(:,3) = []; %This [] is the empty set, it means "delete" here %You could also reach in and define a subset %A = A(:,1:2) %I want to "vectorize" (flatten) matrix A and assign it to B B = A(:) %This destroys the substructure of A and just creates a vector with the same elements %MATLAB operates column-wise by default. If you want the elements of the %vectorized matrix in row-order, you have to transpose first. B = A'; C = B(:) %If you want the *result* to be a row vector, you need to transpose %*afterwards* %Operations on matrices work seamlessly. This is literally one of MATLABs %greatest strengths. They are as fast as can be. %Why? Because MATLAB is slow because it reads and interprets commands line %by line. BUT a single matrix operation is a single line. No matter how big %the matrix is. mean(A) %Without arguments, this is the column-wise mean. mean(A,2) %But you can always say which dimension of the matrix you want to operate on, this gives you the row-wise mean %Again, MATLAB does everything column-default unless you tell it otherwise %MATRIX addition A = [1 2 3;4 5 6;7 8 9] %Stacking row vectors B = ones(3) %This creates a 3x3 square matrix of ones C = A + B %This works as long as they have the same dimensionality %MATRIX multiplication 1: Inner product %Let's say we have an electrode array on every C elegans neuron %C elegans has 302 neurons. We all know them by name. %Let's also pretend that these neurons fired action potentials. %We want to know the expected value of action potentials at any given %time in the C elegans brain, if C elegans had a brain. %Say there are 7 cell classes. neuralFiringRates = [0.5 1 2 4 8 0.25 3]; numberOfNeurons = [45; 15; 35; 20; 50; 36; 101]; %Verify that you have accounted for all the neurons totalNumberOfNeurons = sum(numberOfNeurons) expectedSpikeCount = neuralFiringRates*numberOfNeurons %So the inner product gives us the expected number of spikes %in a single line, regardless of size of nervous system! %What is it doing under the hood? %2 steps: Element-wise multiplication, then summation %Element wise multiplication - the kind you know from high school %is denoted as .* elementWiseMultiplication = neuralFiringRates.*numberOfNeurons' innerProduct = sum(elementWiseMultiplication) %Last thing for today: Holes in array %Nature abhors a vacuum, MATLAB abhors holes in arrays rtP1 = [1000 1500 500 1200] %reaction times per trial, participant 1 rtP2 = [2000 1700 3000 700] %same, participant 2 RTs = [rtP1; rtP2] %Array of RTs where columns are trials and rows are participants mean(RTs,1) %Per trial mean mean(RTs,2) %Per participant mean %What if the 2nd participant missed the last trial? %rtP2 = [2000 1700 3000] %RTs = [rtP1; rtP2] %This throws an error, because MATLAB does not know how to stack %the vectors, which trial is missing. We have to give it a placeholder rtP2 = [2000 1700 3000 nan] RTs = [rtP1; rtP2] %NaN is not a number but is treated by MATLAB as such isnan(rtP2) %tests for whether something is a nan isnumeric(rtP2) %test for whether something is a number %This has implications for the mean: As soon as a single nan %is involved, the result will be a nan. mean(RTs,1) mean(RTs,2) %Once you have nans, to represent the holes, you can call functions %that can handle it: nanmean(RTs,1) nanmean(RTs,2) %Or you could delete the offending trial (or participant) RTs(:,4) = []; mean(RTs,1) mean(RTs,2)
A =
1 2 3
4 5 6
7 8 9
ans =
3 3
ans =
9
ans =
4 5 6
ans =
8 9
ans =
8 9
ans =
1
4
7
ans =
9
B =
1
4
7
2
5
8
C =
1
2
4
5
7
8
ans =
4 5
ans =
1.500000000000000
4.500000000000000
7.500000000000000
A =
1 2 3
4 5 6
7 8 9
B =
1 1 1
1 1 1
1 1 1
C =
2 3 4
5 6 7
8 9 10
totalNumberOfNeurons =
302
expectedSpikeCount =
8.995000000000000e+02
elementWiseMultiplication =
1.0e+02 *
Columns 1 through 3
0.225000000000000 0.150000000000000 0.700000000000000
Columns 4 through 6
0.800000000000000 4.000000000000000 0.090000000000000
Column 7
3.030000000000000
innerProduct =
8.995000000000000e+02
rtP1 =
1000 1500 500 1200
rtP2 =
2000 1700 3000 700
RTs =
1000 1500 500 1200
2000 1700 3000 700
ans =
1500 1600 1750 950
ans =
1050
1850
rtP2 =
2000 1700 3000 NaN
RTs =
1000 1500 500 1200
2000 1700 3000 NaN
ans =
0 0 0 1
ans =
1
ans =
1500 1600 1750 NaN
ans =
1050
NaN
ans =
1500 1600 1750 1200
ans =
1.0e+03 *
1.050000000000000
2.233333333333333
ans =
1500 1600 1750
ans =
1.0e+03 *
1.000000000000000
2.233333333333333