Matlab.md 12.1 KB
 Aaron Erhardt committed May 16, 2021 1 # Matrices  Aaron Erhardt committed Apr 15, 2021 2   Aaron Erhardt committed May 16, 2021 3 ## Generate  Aaron Erhardt committed Apr 15, 2021 4   Aaron Erhardt committed May 16, 2021 5 ### Manual  Aaron Erhardt committed Apr 15, 2021 6 7 8 9 10 11 12 13 14  matlab A = [1 2 3; 4 5 6] A = 1 2 3 4 5 6   Aaron Erhardt committed May 16, 2021 15 ### Zero  Aaron Erhardt committed Apr 15, 2021 16 17  matlab  Aaron Erhardt committed Apr 26, 2021 18 A = zeros(3) % 3x3  Aaron Erhardt committed Apr 15, 2021 19 20 21 B = zeros(3, 2) % 3x2   Aaron Erhardt committed May 16, 2021 22 ### Ones  Aaron Erhardt committed Apr 15, 2021 23 24  matlab  Aaron Erhardt committed Apr 26, 2021 25 A = ones(3) % 3x3  Aaron Erhardt committed Apr 15, 2021 26 27 28 B = ones(3, 2) % 3x2   Aaron Erhardt committed May 16, 2021 29 ### Eye (zeros with ones in the diagonal)  Aaron Erhardt committed Apr 15, 2021 30 31  matlab  Aaron Erhardt committed Apr 26, 2021 32 A = eye(3) % 3x3  Aaron Erhardt committed Apr 15, 2021 33 34 35 B = eye(3, 2) % 3x2   Aaron Erhardt committed May 16, 2021 36 ### Magic  Aaron Erhardt committed Apr 15, 2021 37 38 39 40 41  matlab A = magic(3) % 3x3   Aaron Erhardt committed May 16, 2021 42 ### Random values (from 0 to 1)  Aaron Erhardt committed Apr 15, 2021 43 44 45  matlab A = rand(3) .* 2 % 3x3 with values from 0 to 2  Aaron Erhardt committed Apr 26, 2021 46 B = rand(3, 2) % 3x2  Aaron Erhardt committed Apr 15, 2021 47 48   Aaron Erhardt committed May 16, 2021 49 ### Normally distributed random values (σ = 1, µ = 0)  Aaron Erhardt committed Apr 15, 2021 50 51  matlab  Aaron Erhardt committed Apr 26, 2021 52 53 A = randn(3) .* 2 % 3x3 with σ = 2, µ = 0 B = randn(3, 2) % 3x2  Aaron Erhardt committed Apr 15, 2021 54 55 56 C = randn(3, 2) + 3 % 3x2 with σ = 1, µ = 3   Aaron Erhardt committed May 16, 2021 57 ## Modify  Aaron Erhardt committed Apr 15, 2021 58   Aaron Erhardt committed May 16, 2021 59 ### Transpose  Aaron Erhardt committed Apr 15, 2021 60 61 62 63 64  matlab T = A'   Aaron Erhardt committed May 16, 2021 65 ### Invert  Aaron Erhardt committed Apr 15, 2021 66 67 68 69 70  matlab B = inv(A)   Aaron Erhardt committed May 16, 2021 71 ### Rotate  Aaron Erhardt committed Apr 15, 2021 72 73 74 75 76  matlab B = rot90(A)   Aaron Erhardt committed May 16, 2021 77 ### Flip  Aaron Erhardt committed Apr 15, 2021 78 79  matlab  Aaron Erhardt committed Apr 26, 2021 80 B = flip(A) % flip rows (top to bottom)  Aaron Erhardt committed Apr 15, 2021 81 82 83 B = fliplr(A) % flip columns (left to right)   Aaron Erhardt committed May 16, 2021 84 ### Multiply  Aaron Erhardt committed Apr 15, 2021 85 86 87 88 89  matlab C = A * B   Aaron Erhardt committed May 16, 2021 90 ### Element wise multiplication  Aaron Erhardt committed Apr 15, 2021 91 92 93 94 95  matlab C = A .* B   Aaron Erhardt committed May 16, 2021 96 ## Access  Aaron Erhardt committed Apr 15, 2021 97   Aaron Erhardt committed May 16, 2021 98 ### As row vector  Aaron Erhardt committed Apr 15, 2021 99 100 101 102 103  matlab A(:)   Aaron Erhardt committed May 16, 2021 104 ### Get rows  Aaron Erhardt committed Apr 15, 2021 105 106 107 108 109  matlab A(1, :) % row 1   Aaron Erhardt committed May 16, 2021 110 ### Get columns  Aaron Erhardt committed Apr 15, 2021 111 112 113 114 115  matlab A(:, 1) % column 1   Aaron Erhardt committed May 16, 2021 116 ### Get diagonal  Aaron Erhardt committed Apr 15, 2021 117 118  matlab  Aaron Erhardt committed Apr 26, 2021 119 diag(A) % main diagonal (top left to bottom right)  Aaron Erhardt committed Apr 15, 2021 120 121 122 diag(flip(A)) % other diagonal (bottom left to top right)   Aaron Erhardt committed May 16, 2021 123 ## Linear equations  Aaron Erhardt committed Apr 15, 2021 124 125 126 127 128  matlab x = A \ b % solves Ax = b   Aaron Erhardt committed Jun 23, 2021 129 130 131 132 133 134 135 ## Find indices matlab i = find(A); % find all nonzero elements i = find(A > 1); i = find(A < 1, 5); % get the first five elements   Aaron Erhardt committed Apr 15, 2021 136   Aaron Erhardt committed May 16, 2021 137 # Images  Aaron Erhardt committed Apr 15, 2021 138   Aaron Erhardt committed May 16, 2021 139 ## Load image  Aaron Erhardt committed Apr 15, 2021 140 141 142 143  matlab img = imread('path_to_file')   Aaron Erhardt committed Jun 23, 2021 144 145  ### Show image  Aaron Erhardt committed Apr 15, 2021 146 147 148 149 150  matlab imagesc(img)   Aaron Erhardt committed Jun 23, 2021 151 ### Color map  Aaron Erhardt committed Apr 15, 2021 152 153 154  matlab colormap(gray()) % gray color map  Aaron Erhardt committed Apr 26, 2021 155 colormap(M) % where M 255x3 with values from 0 to 1  Aaron Erhardt committed Apr 15, 2021 156 157 158   Aaron Erhardt committed May 16, 2021 159 160 161 # Datatypes ## Struct  Aaron Erhardt committed Apr 15, 2021 162 163  matlab  Aaron Erhardt committed May 16, 2021 164 165 166 167 student.name = "TestStudent" % create student.age = 24 % add field student2 = struct('name', 'Student2', 'age', 23) % compact disp(student.name) % print name  Aaron Erhardt committed Apr 15, 2021 168 169 170   Aaron Erhardt committed May 16, 2021 171 ## CellArray  Aaron Erhardt committed Apr 15, 2021 172   Aaron Erhardt committed May 16, 2021 173 Array containing different data types  Aaron Erhardt committed Apr 15, 2021 174   Aaron Erhardt committed May 03, 2021 175 ### Create  Aaron Erhardt committed Apr 15, 2021 176 177  matlab  Aaron Erhardt committed May 16, 2021 178 ca = { 'chars', 9.81, 1 } % 1x3 cell array  Aaron Erhardt committed Apr 15, 2021 179 180   Aaron Erhardt committed May 16, 2021 181 ### Access elements  Aaron Erhardt committed Apr 15, 2021 182 183  matlab  Aaron Erhardt committed May 16, 2021 184 ca(1) % -> 'chars'  Aaron Erhardt committed Apr 15, 2021 185 186   Aaron Erhardt committed May 16, 2021 187 ### Get length  Aaron Erhardt committed Apr 15, 2021 188 189  matlab  Aaron Erhardt committed May 16, 2021 190 length(ca)  Aaron Erhardt committed Apr 15, 2021 191 192   Aaron Erhardt committed May 16, 2021 193 194 195 196  ## Table ### Properties  Aaron Erhardt committed Apr 15, 2021 197 198  matlab  Aaron Erhardt committed May 16, 2021 199 200 table.Properties table.Properties.VariableNames{1} % first column name  Aaron Erhardt committed Apr 15, 2021 201 202   Aaron Erhardt committed May 16, 2021 203 ### Access column by name  Aaron Erhardt committed Apr 15, 2021 204 205  matlab  Aaron Erhardt committed May 16, 2021 206 table.column_name % access column (as vector) with name "column_name"  Aaron Erhardt committed Apr 15, 2021 207 208   Aaron Erhardt committed May 16, 2021 209 ### Get sub-table  Aaron Erhardt committed Apr 15, 2021 210 211  matlab  Aaron Erhardt committed May 16, 2021 212 table(:, 2) % second column as table  Aaron Erhardt committed Apr 15, 2021 213 214   Aaron Erhardt committed May 16, 2021 215 ### Access values  Aaron Erhardt committed Apr 15, 2021 216 217  matlab  Aaron Erhardt committed May 16, 2021 218 219 table{1, 2} % row 1, column 2, note brackets! table{:, 2} % column 2  Aaron Erhardt committed Apr 15, 2021 220 221   Aaron Erhardt committed Apr 26, 2021 222   Aaron Erhardt committed May 16, 2021 223 ## Strings  Aaron Erhardt committed Apr 15, 2021 224   Aaron Erhardt committed May 16, 2021 225 ### Create  Aaron Erhardt committed Apr 15, 2021 226 227  matlab  Aaron Erhardt committed May 16, 2021 228 229 str = "Hello" fromChar = string(text)  Aaron Erhardt committed Apr 15, 2021 230 231   Aaron Erhardt committed May 16, 2021 232 ### Concatenate  Aaron Erhardt committed Apr 15, 2021 233   Aaron Erhardt committed May 16, 2021 234 235 236 237 238 239 matlab str2 = str + " World!"  ## Character array  Aaron Erhardt committed Apr 15, 2021 240   Aaron Erhardt committed May 03, 2021 241 ### Create  Aaron Erhardt committed Apr 15, 2021 242 243  matlab  Aaron Erhardt committed May 16, 2021 244 245 text = 'Hello' fromStr = char(str)  Aaron Erhardt committed Apr 15, 2021 246 247   Aaron Erhardt committed May 16, 2021 248 ### Concatenate  Aaron Erhardt committed Apr 15, 2021 249 250  matlab  Aaron Erhardt committed May 16, 2021 251 text2 = [text ' World!']  Aaron Erhardt committed Apr 15, 2021 252 253   Aaron Erhardt committed May 16, 2021 254 ### Access characters  Aaron Erhardt committed Apr 15, 2021 255 256  matlab  Aaron Erhardt committed May 16, 2021 257 c = text(1) % first character  Aaron Erhardt committed Apr 15, 2021 258 259   Aaron Erhardt committed May 16, 2021 260 ### Convert to number  Aaron Erhardt committed Apr 15, 2021 261 262  matlab  Aaron Erhardt committed May 16, 2021 263 str2num('2')  Aaron Erhardt committed Apr 15, 2021 264 265   Aaron Erhardt committed May 16, 2021 266 267 268 ## Output ### Create formatted string  Aaron Erhardt committed Apr 15, 2021 269 270  matlab  Aaron Erhardt committed May 16, 2021 271 sprintf('value: %f', float_val)  Aaron Erhardt committed Apr 15, 2021 272 273   Aaron Erhardt committed May 16, 2021 274 ### Print string  Aaron Erhardt committed Apr 19, 2021 275 276  matlab  Aaron Erhardt committed May 16, 2021 277 disp('Hello')  Aaron Erhardt committed Apr 19, 2021 278 279   Aaron Erhardt committed May 16, 2021 280 ### Print formatted string  Aaron Erhardt committed Apr 15, 2021 281 282  matlab  Aaron Erhardt committed May 16, 2021 283 284 % use \n for newline, the 1 is for printing to stdout fprintf(1, 'value: %d\n', 2)  Aaron Erhardt committed Apr 15, 2021 285 286 287 288 289 290 291 292  # Save and load ## Workspace matlab save file_name  Aaron Erhardt committed Jun 23, 2021 293 save file_name a b c % only save a, b and c  Aaron Erhardt committed Apr 15, 2021 294 295 296 297 298 299 300 301 302 303 load file_name  ## Table matlab table = readtable('file_name') detectImportOptions('file_name') % return detected import options   Aaron Erhardt committed Apr 19, 2021 304 305 306 307 308 309 ## Matrix matlab matrix = readmatrix('file_name') % like readtable but only values as matrix   Aaron Erhardt committed Apr 15, 2021 310 311 312 313 314 315 316 ## File matlab file_handle = fopen('file_name', 'rw') % open as readable and writable fclose(file_handle)   Aaron Erhardt committed May 03, 2021 317   Aaron Erhardt committed Apr 19, 2021 318 319 # Polynomials  Aaron Erhardt committed Jun 23, 2021 320 321 322 323 324 325 ## Create polynomial from roots matlab p = poly([1 -2 3]);   Aaron Erhardt committed Apr 19, 2021 326 327 328 329 330 331 332 333 334 335 336 337 338 339 ## Fit polynomial to points matlab % x and y: coordinate values, deg: degree of polynomial p = polyfit(x, y, deg);  ## Calculate values matlab % get y values for vector x and polynomial x y = polyval(p, x);   Aaron Erhardt committed Jun 23, 2021 340 341 342 343 344 345 346 347 348 349 350 ## Multiply polynomials matlab p3 = conv(p1, p2);  ## Get roots matlab r = roots(p);   Aaron Erhardt committed Apr 19, 2021 351 352 353  # Graphics  Aaron Erhardt committed Apr 26, 2021 354 355 356 ## Plots ### Plot  Aaron Erhardt committed Apr 19, 2021 357 358 359 360 361 362 363 364  matlab x = 1:10; y = x.^2; plot(y); % automatic x-axis plot(x, y); % custom x-axis   Aaron Erhardt committed Apr 26, 2021 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 ### Stairs plot matlab stairs(x, y); % plot values as stairs  ### Discrete values matlab ffty = fft(y); % fast-fourier transform stem(x, abs(ffty)); % plot FFT  ### Logarithmic matlab loglog(x, y); % both axes are logarithmic  ### Semi-logarithmic matlab semilogx(x, y); % x-axes is logarithmic, y-axis is linear semilogy(x, y); % x-axes is linear, y-axis is logarithmic   Aaron Erhardt committed May 03, 2021 391 392 393 394 395 396 397 ### Simple function plot matlab fplot(@sin); % default, from -5 to 5 fplot(@sin, [-pi pi]); % from -pi to pi   Aaron Erhardt committed Apr 26, 2021 398 399 ## Charts  Aaron Erhardt committed May 16, 2021 400 401 402 403 404 405 406 ### Histogram matlab histogram(A) % where A is a vector of values histogram(A, 15) % place values in 15 groups   Aaron Erhardt committed Apr 26, 2021 407 408 409 410 411 412 413 414 415 ### Bar matlab h = bar([1 2 4 8]); % create bar chart h.FaceColor = [0, 0.7 0]; % set properties  ## Plot options  Aaron Erhardt committed Apr 19, 2021 416 417 418 419 420 421 422 423 424 ### Hold (keep previous plots) matlab hold on plot(...)  ### Formatting  Aaron Erhardt committed Apr 26, 2021 425 Markers  Aaron Erhardt committed Apr 19, 2021 426 427 428 429 430 431 432 433  matlab plot(x, y, '.r'); % red dots plot(x, y, 'or'); % red circles plot(x, y, 'xr'); % red x plot(x, y, '*r'); % red stars   Aaron Erhardt committed Apr 26, 2021 434 Lines  Aaron Erhardt committed Apr 19, 2021 435 436 437 438 439 440 441 442  matlab plot(x, y, '-'); % regular lines plot(x, y, ':'); % dotted lines plot(x, y, '--'); % dashed lines plot(x, y, '-.'); % dashed and dotted lines   Aaron Erhardt committed Apr 26, 2021 443 Colors  Aaron Erhardt committed Apr 19, 2021 444 445 446 447 448  matlab plot(x, y, 'color', [0, 0.6, 0.2]); % RGB color   Aaron Erhardt committed Apr 26, 2021 449 Line width  Aaron Erhardt committed Apr 19, 2021 450 451 452 453 454  matlab plot(x, y, 'LineWidth', 2); % set line width to 2 px   Aaron Erhardt committed Apr 26, 2021 455 Marker color  Aaron Erhardt committed Apr 19, 2021 456 457 458 459 460  matlab plot(x, y, 'MarkerFaceColor', [0, 0.6, 0.2]); % RGB color of markers   Aaron Erhardt committed Apr 26, 2021 461 Marker size  Aaron Erhardt committed Apr 19, 2021 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485  matlab plot(x, y, 'MarkerSize', 7); % set marker size to 7 px  ### Legend and title matlab title('Title'); % number of names should match number and order of plots legend('First', 'Second', 'Third'); % legend with specific location legend('First', 'Second', 'Third', 'Location', 'northwest');  ### Labels matlab xlabel('\alpha'); % set x-label to α ylabel('y');   Aaron Erhardt committed Apr 26, 2021 486 487 488 489 490 491 ### Axes matlab axis([XMIN XMAX YMIN YMAX]); % set scaling of both axes   Aaron Erhardt committed Apr 19, 2021 492 493 494 ## Figures matlab  Aaron Erhardt committed Apr 26, 2021 495 496 fig = figure; % note: properties are case sensitive! fig = gcf; % get handle to current figure  Aaron Erhardt committed May 16, 2021 497 clf; % clear current figure  Aaron Erhardt committed Apr 26, 2021 498 499 500 501 502  ### Typical object hierarchy mermaid  Aaron Erhardt committed Apr 26, 2021 503 504 505 506 graph TD Figure --> Legend Figure --> Axes Axes --> Lines  Aaron Erhardt committed Apr 26, 2021 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521  ### Set color matlab fig.Color = 'w'; % white fig.Color = [1.0 0.5 0.0]; % RGB  ### Plot handles matlab h = plot(x, y); % get handle h.LineWidth = 1.3; % modify properties h.Marker = '.'; % set marker  Aaron Erhardt committed Apr 19, 2021 522 523   Aaron Erhardt committed Apr 26, 2021 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 ### Sub-plots matlab fig = figure; subplot(2, 3, 1); % 2x3 matrix of plots, select first plot plot(x, y); % first plot subplot(2, 3, 2); % 2x3 matrix of plots, select second plot loglog(x, y); % second plot subplot(2, 3, 4); % 2x3 matrix of plots, select fourth plot semilogx(x, y); % second row, first plot subplot(2, 3, 6); % 2x3 matrix of plots, select last plot semilogy(x, y); % second row, third plot  Get handle matlab handle = subplot(2, 3, 1); % select first plot handle.Children(1).LineWidth = 2; % get line from handle and change width   Aaron Erhardt committed May 16, 2021 548 549 550 551 552 553 554 555 ## 3-dimensional ### 3D-plot matlab plot3(x, y, z);   Aaron Erhardt committed May 16, 2021 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 ### Surfaces Generate grid matlab % Generate grid from matrices with duplicate rows of x and duplicate columns of y [X Y] = meshgrid(x, y); % Matrices with dimension length(x) x length(y) mesh(X, Y) % Plot mesh Z = X.^2 - Y % use (uppercase!) X and Y to calculate Z mesh(X, Y, Z) % Plot mesh with Z coordinate  Mesh surface plot matlab mesh(X, Y) % Plot mesh (using X and Y from meshgrid) Z = X.^2 - Y % use (uppercase!) X and Y to calculate Z mesh(X, Y, Z) % Plot mesh with Z coordinate  Surface plot  Aaron Erhardt committed May 16, 2021 579 580  matlab  Aaron Erhardt committed May 16, 2021 581 surf(X, Y, Z) % Similar to mesh but always with Z coordinate  Aaron Erhardt committed May 16, 2021 582   Aaron Erhardt committed Apr 26, 2021 583   Aaron Erhardt committed May 03, 2021 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 # Functions ## Regular functions ### Definition matlab % global function, always at the top of a file function file_name() % code... end % local function function FuncName() % code... end  ### Parameters matlab function funcName(param1, param2) disp(param1); % print param1 disp(param2); % print param2 end  Variable parameters matlab function funcName(param1, param2, varargin) % function takes at least two parameters for cnt = 1:length(varargin) arg = varargin{cnt}; % get one of the variable arguments if isnumeric(arg) % only print numeric values disp(arg); % print variable parameters else % ignore non-numeric values disp("Can't display non-numeric values"); end end end  ### Return values matlab function returnValue = duplicate(param) returnValue = [param param]; end value = duplicate(2) % value = [2 2] % multiple return values function [returnValue1, returnValue2] = funcName(param1, param2) returnValue1 = param1; returnValue2 = param2; end [value1, value2] = funcName(2, 3) % value1 = 2, value2 = 3  ### Documentation matlab function funcName() % This is a documentation comment % Explain what your function does here % This is a normal comment again end  ## Anonymous functions ### Get function handle matlab handle = @sin handle(1) % returns sin(1)  ### Define anonymous function matlab handle = @(param) 2 * cos(param * 2) % handle(1) returns 2*cos(2) handle = @(x) exp(x-x.^2) - 0.5  ## Function characteristics ### Zeros matlab fzero(handle, 1) % find a zero value starting from 1  ### Fit parameters matlab id = @(is,ud)is*exp(ud/0.05) % diode current is = lsqcurvefit(id, 1e-10, u, i); % find ideal value for is to fit measured values u and i   Aaron Erhardt committed May 16, 2021 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 ## Differential equations ### First order matlab % transform the equation so that u' is separated du = @(t,u) -u/0.1; % u' = -u/0.1 U0 = 3; % start value tspan = [0 0.5]; % time span [t u] = ode45(du, tspan, U0); % calculate values  ### Higher order matlab % transform the equation so that y' = y1' and y'' = y2' are separated % y1' = y2 y2' = sin(100*t) - y1 - y2 dy = @(t,y) [ y(2) ; sin(100*t) - y(1) - y(2) ]; % c) [t y] = ode45(dy, [0 1], [1; 0]); % start at y1 = 1 and y2 = 0  ### Options matlab opts = odeset('absTol', 0.05); % set absolute error tolerance [t y] = ode45(@pendfun, tspan, yNull, opts);   Aaron Erhardt committed Jun 23, 2021 717 718 719 720  # Transfer functions ## Create  Aaron Erhardt committed May 16, 2021 721 722  matlab  Aaron Erhardt committed Jun 23, 2021 723 G = tf([1 0], [2 0 0]);  Aaron Erhardt committed May 16, 2021 724 725   Aaron Erhardt committed Jun 23, 2021 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 ## Step response information matlab S = stepinfo(G);  ## Plot step response matlab orstep(G); step(G, 0.2);  ## Plot bode diagram matlab bode(G);  ## Plot nyquist diagram matlab nyquist(G);   Aaron Erhardt committed May 16, 2021 750   Aaron Erhardt committed Apr 15, 2021 751 752 753 754 755 756 # Control structures ## for-loop matlab for cnt = 1:5 % cnt can be any row vector  Aaron Erhardt committed Apr 19, 2021 757  fprintf(1, '%d\n', cnt);  Aaron Erhardt committed Apr 15, 2021 758 759 760 761 762 763 764 765 end  ## while-loop matlab cnt = 0; while cnt < 5  Aaron Erhardt committed Apr 19, 2021 766  fprintf(1, '%d\n', cnt);  Aaron Erhardt committed Apr 15, 2021 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783  cnt = cnt + 1; end  ## if-statement matlab cnt = 0; if cnt == 0 disp('Zero'); elseif mod(cnt, 2) == 0 disp('Even'); else disp('Odd'); end   Aaron Erhardt committed Apr 26, 2021 784 Comparing vectors  Aaron Erhardt committed Apr 15, 2021 785 786 787 788 789 790 791 792 793 794 795 796 797  matlab vec1 = [1 2 3]; vec2 = [1 2 4]; vec1 == vec2 % 1×3 logical array % 1 1 0 if ~isequal(vec1, vec2) disp('Not equal') end   Aaron Erhardt committed Apr 19, 2021 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814  ## switch-statement matlab value = 4; switch value case 1 display("One"); case 2 display("Two"); case 3 display("Three"); otherwise display("Other number"); end   Aaron Erhardt committed May 03, 2021 815   Aaron Erhardt committed Apr 19, 2021 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 # Performance ## Time measurment matlab tic % start time measurement time = toc % take time measurement time2 = toc % take another time measurement (from same tic)  ## Solving linear equations matlab x = A\b; % fast, because optimized by Matlab x = inv(A)*b; % slow  ## Pre-allocating vectors matlab for cnt = 1:100 % inefficient, causes a lot of re-allocations a(cnt) = cnt; end b = zeros(1, 100); % pre-allocating memory for cnt = 1:100 a(cnt) = cnt; end  ### Real-world example matlab x = 0:0.01:150; y = zeros(size(x)); % initialized with zeros, same size as x cnt = 1; for i = x y(cnt) = sin(i); cnt = cnt + 1; end  Aaron Erhardt committed Jun 23, 2021 857