Docsity

Programming - Unit 4: Arrays (slides), Diapositivas de Programación Informática

Vista previa parcial del texto

¡Descarga Programming - Unit 4: Arrays (slides) y más Diapositivas en PDF de Programación Informática solo en Docsity!

U N I T 4.

A R R A Y S

P R O G R A M M I N G. A E R O S P A C E E N G I N E E R I N G Y E A R 2 0 2 0 - 2 0 2 1

CONTENTS

1. Structured data types vs simple data types

2. Concept of array: array, vector and matrix

3. Use of arrays

1. Declare and initialise an array
2. Access array elements: concept of index
3. Reading and displaying array elements

4. Arrays in MatLab

1. Matrix and arrays operations
2. Array indexing in MatLab
3. Multi dimensional arrays
4. Preallocation
5. Vectorization

STRUCTURED DATA TYPES VS SIMPLE DATA TYPES

  • Unstructured (simple) data type: scalars
    • Data with a single element and a single value
      • Numbers: integer , float
      • Characters: char
  • Structured data types
    • Data with an internal structure, not a single element
      • Arrays
      • Structures
        • Structured vs simple data types

C O N C E P T O F A R R AY

A R R AY – V E C TO R - M AT R I X

DATA STRUCTURE TO STORE THE DAILY TEMPERATURE IN MADRID

  • All elements of the same type (double)
  • Share a name: temperature
  • Each element has a different value
  • Each element is identified with and index: 0,1,…., 365
  • Use the index to access the element
    • E.g.: Assign temperature in day 3 6.5 9.0 10.7 ............ 1. temperature (3) = 10.7; 1 2 3 365 temperature (^) One dimension = vector

DATA STRUCTURE TO STORE OCCUPATION OF A MOVIE THEATRE

  • All elements share the name (theatre)
  • Individual elements are identified by the indexes
    • In this case index will be row and column
      • theatre (1,1) = 1;
      • theatre (3,4)= 1; 1 1 0 1 1 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 Two dimensions Row Column
theatre 4x
0 free, 1 occupied

DIMENSIONS OF AN ARRAY

  • One-dimensional array: vector
    • In MatLab row vectors and column vectors
  • Two dimensions: matrix
    • table, with rows and columns
    • MatLab is short for Matrix Laboratory
      • designed to work with matrices
      • all variables in MatLab are matrices
  • More than two dimensions: multidimensional array 1 2 3 4 5 6 7 8
row vector
column vector

MATLAB DATA TYPES REVISITED

Scalars: atomic information, single values In matlab, scalars are 1x1 arrays Structured information All data in matlab are arrays

DECLARING AN ARRAY

  • Features that define an array
    • Data type of the array elements
    • Name of the array
    • Number of dimensions
    • Number of elements for each dimension
  • Datatypes of array elements
  • Array elements can be
  • scalar: numeric, char
  • Structured data

DECLARING VECTORS

ROW V EC TOR

  • Listing the values rowVector = [12 14 63] rowVector = [12, 14, 63]
  • Defining a range v3= [1:1:100] % 1 can be omitted v4=[1:3:100]
  • Using functions a =ones(1,10) b =zeros(1,10) COL UMN V ECTOR
  • Listing the values columnVector = [12; 14;63]
  • Using functions a =ones(10,1) b =zeros(10,1)
Coma , Semicolon ;

DECLARING A MATRIX

  • Using functions
a = ones (3,5)
b = zeros(3,5)
C = randi (10, 3,5)

b = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 C = 1 10 10 9 10 3 10 10 2 8 6 2 5 5 10 a = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

U S E O F A R R AY S

AC C E S S A R R AY E L E M E N T S :

I N D E X

vector =[4, 8, 15, 16, 23, 42]; % row vector, 6 elements
fprintf (%i',vector(7));

OUT OF RANGE ARRAY ACCESS

  • What happens if we try to use an element of the array beyond its boundaries?
Error: Index exceeds the
number of array elements

U S E O F A R R AY S

R E A D I N G A N D D I S P L AY I N G

A R R AY E L E M E N T S