Insight Through Computing 20. More on 2D Arrays And their connections to Cell arrays Structure arrays Character arrays.

Slides:



Advertisements
Similar presentations
February 26, 2015Applied Discrete Mathematics Week 5: Mathematical Reasoning 1 Addition of Integers Example: Add a = (1110) 2 and b = (1011) 2. a 0 + b.
Advertisements

MATLAB Examples. CS 1112 MATLAB Examples Find the number of positive numbers in a vector x = input( 'Enter a vector: ' ); count = 0; for ii = 1:length(x),
Maths for Computer Graphics
Multidimensional arrays Many problems require information be organized as a two- dimensional or multidimensional list Examples –Matrices –Graphical animation.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Insight Through Computing 22. Working with Image Files imread, imwrite, imshow, uint8, rgb2gray.
Connecting with Computer Science, 2e
Insight Through Computing 18. Two-Dimensional Arrays Set-Up Rows and Columns Subscripting Operations Examples.
CSE202: Lecture 16The Ohio State University1 Two Dimensional Arrays.
1 Matrix Addition, C = A + B Add corresponding elements of each matrix to form elements of result matrix. Given elements of A as a i,j and elements of.
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
MATRICES. Matrices A matrix is a rectangular array of objects (usually numbers) arranged in m horizontal rows and n vertical columns. A matrix with m.
Bits and Bytes.
Connecting with Computer Science 2 Objectives Learn why numbering systems are important to understand Refresh your knowledge of powers of numbers Learn.
Row 1 Row 2 Row 3 Row m Column 1Column 2Column 3 Column 4.
February 25, 2002Applied Discrete Mathematics Week 5: Mathematical Reasoning 1 Addition of Integers How do we (humans) add two integers? Example: 7583.
Matlab tutorial course Lesson 2: Arrays and data types
Numbering Systems CS208.
The fifty Cent Version of Digital Imaging Bits Bytes Pixels Matrix Dynamic range Machine language Processors (8,10,12 bits etc.) Base 10 numbering Binary.
Different Types of Fields to Describe the Earth. Anisotropy, heterogeneity SEM of shale (Josh et al., 2012)
Row 1 Row 2 Row 3 Row m Column 1Column 2Column 3 Column 4.
Lecture 7 Matrices CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.
Chapter 4 – Matrix CSNB 143 Discrete Mathematical Structures.
Two Dimensional Arrays
Multidimensional Arrays. 2 Two Dimensional Arrays Two dimensional arrays. n Table of grades for each student on various exams. n Table of data for each.
Slide Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Visualizing Decimal and Binary
ME 142 Engineering Computation I Matrix Operations in Excel.
COMPUTER GRAPHICS. Can refer to the number of pixels in a bitmapped image Can refer to the number of pixels in a bitmapped image The amount of space it.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Number Representation Lecture Topics How are numeric data items actually stored in computer memory? How much space (memory locations) is.
MT411 Robotic Engineering Asian Institution of Technology (AIT) Chapter 1 Introduction to Matrix Narong Aphiratsakun, D.Eng.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
Binary 101 Gads Hill School. Aim To strengthen understanding of how computers use the binary number system to store information.
ECE 103 Engineering Programming Chapter 23 Multi-Dimensional Arrays Herbert G. Mayer, PSU CS Status 6/24/2014 Initial content copied verbatim from ECE.
ELECTRICAL CIRCUITS. Circuits Circuits are paths which small particles flow through to provide us with electricity for lights, appliances, and other devices.
Matrix Algebra Definitions Operations Matrix algebra is a means of making calculations upon arrays of numbers (or data). Most data sets are matrix-type.
Chapter 1 Computing Tools Variables, Scalars, and Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Number systems Visualizing Decimal and Binary. We count in base 10 because people started by counting on their fingers Base 10 is a number system that.
Ch. 12 Vocabulary 1.) matrix 2.) element 3.) scalar 4.) scalar multiplication.
Electric Circuits.
The Euclidean Algorithm
CSNB 143 Discrete Mathematical Structures
13.4 Product of Two Matrices
Sections 2.4 and 2.5 Matrix Operations
Properties and Applications of Matrices
Warm-up Problem Use the Laplace transform to solve the IVP.
CHAPTER 2 MATRICES Determinant Inverse.
Applied Discrete Mathematics Week 5: Mathematical Reasoning
Matrix Representation of Graphs
Applied Discrete Mathematics Week 4: Number Theory
Rosen 5th ed., §2.7 ~18 slides, ~1 lecture
Sequences and Summations
3.9 Determinants det(A) or |A| a b a b det = = ad - bc c d c d
Dr Huw Owens Room B44 Sackville Street Building Telephone Number 65891
CSE 541 – Numerical Methods
Matrix Operations and Their Applications
Matrices Introduction.
CS100: Discrete structures
How Computers Store Data
Lesson 4: Introduction to Functions
And their connections to Cell arrays Structure arrays Character arrays
14.1 Vectors in Two Dimensions
Discrete Mathematics and its Applications
Matrices An appeaser is one who feeds a crocodile—hoping it will eat him last. Winston Churchhill.
Matrix Addition, C = A + B Add corresponding elements of each matrix to form elements of result matrix. Given elements of A as ai,j and elements of B as.
Electronic Memory.
Rosen 5th ed., §2.7 ~18 slides, ~1 lecture
Arrays and Matrices Prof. Abdul Hameed.
Applied Discrete Mathematics Week 4: Functions
Presentation transcript:

Insight Through Computing 20. More on 2D Arrays And their connections to Cell arrays Structure arrays Character arrays

Insight Through Computing Application: digital displays

Insight Through Computing 7-by-5 “dot matrices”

Insight Through Computing A “bit map” for each digit A “light” is either on or off. A 7-by-5 matrix of zeros and ones can “tell the whole story.”

Insight Through Computing Computing with these bitmaps What is a good scheme for storing the 10 bitmaps? How to draw one digit? How to display a number? Other interesting questions: How to draw a mirror image of a digit? Which “light bulb” switches on most often?

Insight Through Computing Design decisions… How do we package a particular digit? numerical array or character array How do we package the collection of digits? cell array or structure array

Insight Through Computing [ ;… ;… ;… ;… ;… ;… ]; Can use a numerical array for each digit

Insight Through Computing [ ‘01110’;… ‘10001’;… ‘00010’;… ‘00100’;… ‘01000’;… ‘10000’;… ‘11111’]; Can use a character array for each digit

Insight Through Computing M = [ ;… ;… ;… ;… ;… ;… ]; D{2} = M; Can use a cell array to keep the 10 bitmaps Each cell of cell array D is a numerical matrix.

Insight Through Computing % given 1<=k<=10 M = D{k}; if M(4,3)==1 disp(‘Middle light is on’) end With D{1},…,D{10} set up as a cell array of numerical matrices, can do computation as follows:

Insight Through Computing M = [‘01110’;… ‘10001’;… ‘00010’;… ‘00100’;… ‘01000’;… ‘10000’;… ‘11111’]; D{2} = M; Still using a cell array to keep the 10 bitmaps Each cell of cell array D is a matrix of characters.

Insight Through Computing % given 1<=k<=10 M = D{k}; if strcmp(M(4,3),’1’) disp(‘Middle light is on’) end With D{1},…,D{10} set up as a cell array of character matrices, can do computation as follows: [‘01110’;… ‘10001’;… ‘00010’;… ‘00100’;… ‘01000’;… ‘10000’;… ‘11111’];

Insight Through Computing Numerical matrix for each digit Which do you prefer? A B [ ;… ;… ;… ;… ;… ;… ]; Character matrix for each digit [ ‘01110’;… ‘10001’;… ‘00010’;… ‘00100’;… ‘01000’;… ‘10000’;… ‘11111’];

Insight Through Computing M = [ ;… ;… ;… ;… ;… ;… ]; D(2) = struct(‘map’, M); Can use a structure array to keep the 10 bitmaps Each component of array D is a structure, and the sole field in the structure is a matrix of numbers.

% Example for the digit 2 A = [ ;… ;… ;… ;… ;… ;… ]; D(2)= struct(‘map’, A); Using a structure array to keep the 10 bitmaps… The k-th component of array D encodes digit k. M = D{k}; if M(4,3)==1 disp(‘Middle light on’) end Which fragment on the right is correct given 1<=k<=10 ? M = D(k); if M(4,3)==1 disp(‘Middle light on’) end A M = D(k).map; if M(4,3)==1 disp(‘Middle light on’) end B C M = D{k}.map; if M(4,3)==1 disp(‘Middle light on’) end D

Insight Through Computing % given 1<=k<=10 M = D(k).map; if M(4,3)==1 disp(‘Middle light is on’) end With D(1),…,D(10) set up as a structure array of numerical matrices, can do computation as follows:

Insight Through Computing M = [ ‘01110’;… ‘10001’;… ‘00010’;… ‘00100’;… ‘01000’;… ‘10000’;… ‘11111’]; D(2) = struct(‘map’, M); Can use a structure array to keep the 10 bitmaps Each component of array D is a structure, and the sole field in the structure is a matrix of characters.

Insight Through Computing % given 1<=k<=10 M = D(k).map; if strcmp(M(4,3),’1’) disp(‘Middle light is on’) end With D(1),…,D(10) set up as a structure array of character matrices, can do computation as follows: [‘01110’;… ‘10001’;… ‘00010’;… ‘00100’;… ‘01000’;… ‘10000’;… ‘11111’];

Insight Through Computing Choice for storing the bit maps Cell array better than struct array No point in having a structure with one field Numerical array better than char array Plan on doing numerical computations with the bit maps—char arrays not handy

Insight Through Computing function D = TheDigits() % D is a 10-by-1 cell array where D{k} is a 7-by-5 matrix % that encodes digit k. D{10} encodes 0. D = cell(10,1); D{1} = [ ; ; ; ]; D{2} = [ ; ; ; ; ; ; ]; ⋮ Given this function, can write other functions to draw a single digit, multiple digits, etc. See also showDigits.m

Insight Through Computing Produce a cell array of “reverse” digits For every digit (matrix), need to reverse the order of the columns.

Insight Through Computing Reversing Column Order Suppose A has 5 columns. If B(:,1) = A(:,5) B(:,2) = A(:,4) B(:,3) = A(:,3) B(:,4) = A(:,2) B(:,5) = A(:,1) then B is A with its cols reversed. B(:,k) = A(:,6-k)

Insight Through Computing function B = reverseCol(A) % B is a matrix obtained by reversing % the order of the columns in matrix A [nr, nc]= size(A); B= zeros(nr,nc); for k= 1:nc B(:,k) = A(:,nc-k+1); end

Insight Through Computing function revD = reverseDigits() % revD is a 10-by-1 cell array. % revD{k} is the reversed 7-by-5 bitmap % of digit k. revD{10} encodes 0. D= TheDigits(); revD= cell(10,1);

function revD = reverseDigits() % revD is a 10-by-1 cell array. % revD{k} is the reversed 7-by-5 bitmap % of digit k. revD{10} encodes 0. D= TheDigits(); revD= cell(10,1); for k= 1:length(D) M= D{k}; revM= reverseCol(M); revD{k}= revM; end

Insight Through Computing How to calculate the difference between 2 bitmaps? – = ABC C(i,j) = abs( A(i,j) - B(i,j) )

% A and B have same size [nr,nc]= size(A); B= zeros(nr,nc); for r= 1:nr for c= 1:nc C(r,c)= abs(A(r,c)-B(r,c)); end % A and B have same size C= abs(A-B); C is a 0-1 matrix where 1 indicates that A(i,j) and B(i,j) are different.

Insight Through Computing Adding Up The Changes D = TheDigits(); Count = zeros(7,5); n = 10000; for k=1:n i1 = ceil(10*rand); i2 = ceil(10*rand); % M(i,j) = 1 if the two bitmaps disagree in % position (i,j). M = Difference(D{i1},D{i2}); Count = Count + M; end

Insight Through Computing Results