Chapter 3. Arrays and Array Operations

Chapter 3. Arrays and Array Operations

Chapter 3. Arrays and Array Operations All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807. 1 out of 42 Engineering Computations and Modeling in MATLAB/Simulink Outline • 3.1 Introduction • 3.2 Types of Arrays and Indexing Their Elements • 3.3 Array Operations • 3.4 Array Functions • 3.5 Using MATLAB Matrix Formalism to Handle Polynomials • 3.6 Handling Character and String Arrays All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807. 2 out of 42 Engineering Computations and Modeling in MATLAB/Simulink Classification of Arrays 2D Array (Matrix) nD Array a = [ ] The Empty Matrix (0-by-0 matrix) Scalar (1-by-1 matrix) Vector (1-by-n or n-by-1 matrix) Raw Vector (1-by-n matrix) Column Vector (n-by-1 matrix) Zero Vector Unit Vector Zero Vector Unit Vector Matrix (n-by-m matrix) Rectangular Matrix (n-by-m matrix) Square Matrix (n-by-n matrix) Diagonal Matrix Triangular Matrix Magic Matrix Orthogonal Matrix Identity Matrix Block Diagonal Matrix Orthonormal Matrix All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807. 3 out of 42 Engineering Computations and Modeling in MATLAB/Simulink Matrices and 3-D Arrays Row 1 a11 a 12 a 13 a 14 a 15 a 16 Row 2 a a a a a a 21 22 23 24 25 26 a ==[]aij Row 3 a31 a 32 a 33 a 34 a 35 a 36 a41 a 42 a 43 a 44 a 45 a 46 Row 4 Col 1 Col 2 Col 3 Col 4 Col 5 Col 6 All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807. 4 out of 42 Engineering Computations and Modeling in MATLAB/Simulink Creating Arrays: Quick Start All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807. 5 out of 42 Engineering Computations and Modeling in MATLAB/Simulink Addressing a Single Element 1D arrays Row 1 Row 1 Page 5 Row 2 Row 1 Page 4 Row 3 Col 1 Col 2 Col 3 Col 4 Col 5 Col 6 Row 1 Page 3 Row 4 a()1,5 a()4,1 2D array Row 1 Page 2 Col 1 Col 2 Col 3 Col 4 Col 5 Col 6 Row 1 Row 1 Page 1 Row 2 Row 2 Col 1 Col 2 Col 3 Col 4 Col 5 Col 6 Row 3 Row 3 Col 1 Col 2 Col 3 Col 4 Col 5 Col 6 Row 4 Row 4 Col 1 Col 2 Col 3 Col 4 Col 5 Col 6 3D array a()2,4 Col 1 Col 2 Col 3 Col 4 Col 5 Col 6 Col 1 Col 2 Col 3 Col 4 Col 5 Col 6 a()1,4,4 All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807. 6 out of 42 Engineering Computations and Modeling in MATLAB/Simulink Arrays in Computer Memory 1 a(1,1) 5 a(2,1) 1 2 3 4 9 a(3,1) 5 6 7 8 2 a(1,2) 9 10 11 12 6 a(2,2) 10 a(3,2) a()3, 2 a()6 3 a(1,3) 7 a(2,3) i=sub2ind(size(a),3,2) returns i=6 11 [r,c]=ind2sub(size(a),6) returns r=3 and c=2 a(3,3) 4 a(1,4) size(a) returns [3 4] 8 a(2,4) length(a) returns 4 (it is equivalent to max(size(X))) ndims(a) returns 2 12 a(3,4) numel(a) returns 12 All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807. 7 out of 42 Engineering Computations and Modeling in MATLAB/Simulink Addressing Multiple Elements a(1,1) a(2,2) a([9 13; 12 16]) a([17 21; 18 22]) a(1) a(6) a([1 4],[3 4]) a(1:2,5:6) a(3,1:6) Row 1 a(3,1:end) a(3,:) Row 2 a([3 7 11 15 19 23]) a([3:4:23]) Row 3 a(3,[1,2,3,4,5,6]) Row 4 a(4,6) a(24) Col 1 Col 2 Col 3 Col 4 Col 5 Col 6 a(end) a([8,10]) a(sub2ind(size(a),[4 2],[2 3])) All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807. 8 out of 42 Engineering Computations and Modeling in MATLAB/Simulink Creating Special Arrays 1) ones(m,n,p,…) creates an mxnxpx… array of ones (ones(n)creates an n x n matrix of ones) 2) zeros(m,n,p,…) creates an mxnxpx… array of zeros (zeros(n) creates an n x n matrix of zeros, called a null matrix and denoted by 0) 3) eye(m,n) creates an mxn matrix with with 1’s on the diagonal and zeros elsewhere (eye(n) creates a square nxn matrix, called identity matrix and denoted by I, where all the elements are zero except the diagonal elements, which are unity) 4) magic(n) creates a magic square constructed from the integers 1 through n2 with equal row, column, and diagonal sums (produces valid magic squares for all n > 0 except n = 2) 5) pascal(n) creates the Pascal matrix of order n (a symmetric positive definite matrix with integer entries), where any element is computed from Pascal’s triangle, i.e. ai;j=ai-1;j+ai;j-1 >> ones(1,4) >> magic(6) ans = ans = 1 1 1 1 35 1 6 26 19 24 3 32 7 21 23 25 >> eye(2,5) 31 9 2 22 27 20 ans = 8 28 33 17 10 15 1 0 0 0 0 30 5 34 12 14 16 0 1 0 0 0 4 36 29 13 18 11 All rights reserved. No part of this publication may be reproduced, distributed, or transmitted, unless for course participation, in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the Publisher and/or Author. Contact the American Institute of Aeronautics and Astronautics, Professional Development Programs, 12700 Sunrise Valley Drive, Suite 200, Reston, VA 20191-5807. 9 out of 42 See gallery for creating more test matrices Engineering Computations and Modeling in MATLAB/Simulink Assessing Array Properties 1) ndims(A) returns the number of dimensions in the array A 2) size(A) returns a row vector whose elements contain the length of the corresponding dimension of A (size(A,dim) returns the length of dimension dim; [m,n] = size(A),…) returns the number of rows and columns when A is a matrix) 3) length(A) returns the length of the largest array dimension in A (for vectors the length is simply the number of elements, otherwise it is equivalent to max(size(X)) ) 4) numel(A) returns the number of elements in array A (equivalent to prod(size(A))) >> A=pascal(3) A = >> ndims(A) >> size(A) 1 1 1 ans = ans = 1 2 3 2 3 2 1 3 6 >> A(:,1)=[] A = >> length(A) >> numel(A) 1 1 ans = ans = 2 3 3 6 3 6 All rights reserved.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    42 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us