Additional Data Types: 2-D Arrays, Logical Arrays, Strings Selim Aksoy Bilkent University Department of Computer Engineering

Slides:



Advertisements
Similar presentations
Multidimensional Array
Advertisements

COMP 116: Introduction to Scientific Programming Lecture 37: Final Review.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
DREAM PLAN IDEA IMPLEMENTATION. 2 Present to: Amirkabir University of Technology (Tehran Polytechnic) & Semnan University Dr. Kourosh Kiani
Chapter 7. Even in quantitative sciences, we often encounter letters and/or words that must be processed by code You may want to write code that: Reads.
Maths for Computer Graphics
String Functions. Getting Numeric Equivalents of Chars >> double('abc xyz') ans = >> double('ABC XYZ') ans =
CIS 101: Computer Programming and Problem Solving Lecture 7 Usman Roshan Department of Computer Science NJIT.
Arrays Revisited Selim Aksoy Bilkent University Department of Computer Engineering
General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009.
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
MATLAB Review Selim Aksoy Bilkent University Department of Computer Engineering
Top-down Program Design, Relational and Logical Operators
Week 3 Last week Vectors Array Addressing Mathematical Operations Array Multiplication and Division Identity Matrix Inverse of a Matrix Element by Element.
User-defined Functions Selim Aksoy Bilkent University Department of Computer Engineering
Top-down Program Design Selim Aksoy Bilkent University Department of Computer Engineering
Program Design, Relational and Logical Operators Selim Aksoy Bilkent University Department of Computer Engineering
Insight Through Computing 15. Strings Operations Subscripting Concatenation Search Numeric-String Conversions Built-Ins: int2str,num2str, str2double.
Additional Data Types: 2-D Arrays, Logical Arrays Selim Aksoy Bilkent University Department of Computer Engineering
Introduction to MATLAB
Additional Data Types: Strings Selim Aksoy Bilkent University Department of Computer Engineering
MATLAB Strings Selim Aksoy Bilkent University Department of Computer Engineering
Relational and Logical Operators Selim Aksoy Bilkent University Department of Computer Engineering
CE 311 K - Introduction to Computer Methods Daene C. McKinney
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Text Processing. Outline Announcements: –Homework I: due Today. by 5, by Discuss on Friday. –Homework II: on web HW I: question 7 Finish functions.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (3): MATLAB Environment (Chapter 1)
CSE123 Lecture 6 String Arrays. Character Strings In Matlab, text is referred to as character strings. String Construction Character strings in Matlab.
1 Week 3: Vectors and Matrices (Part III) READING: 2.2 – 2.4 EECS Introduction to Computing for the Physical Sciences.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,
Array Addition  Two arrays can be added if and only if both arrays have exactly the same dimensions.  Assuming the dimension requirement is satisfied,
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Learner’s Guide to MATLAB® Chapter 2 : Working with Arrays.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
COMP 116: Introduction to Scientific Programming Lecture 28: Midterm #2 Review.
CMPS 1371 Introduction to Computing for Engineers MATRICES.
Lecture 28: Mathematical Insight and Engineering.
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
COMP 116: Introduction to Scientific Programming Lecture 24: Strings in MATLAB.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
A string is an array of characters Strings have many uses in MATLAB Display text output Specify formatting for plots Input arguments for some functions.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
Matlab Basic. MATLAB Product Family 2 3 Entering & Quitting MATLAB To enter MATLAB double click on the MATLAB icon. To Leave MATLAB Simply type quit.
MT411 Robotic Engineering Asian Institution of Technology (AIT) Chapter 1 Introduction to Matrix Narong Aphiratsakun, D.Eng.
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
CMPS 1371 Introduction to Computing for Engineers CHARACTER STRINGS.
1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Strings Characters and Sentences 1.Overview 2.Creating Strings 3.Slicing Strings 4.Searching for substrings 1.
Finishing up Chapter 5. Will this code enter the if statement? G=[30,55,10] if G
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Part 3 - Chapter 8 Linear Algebraic Equations and Matrices.
Matrices. Variety of engineering problems lead to the need to solve systems of linear equations matrixcolumn vectors.
Characters and Text You can assign text strings to MATLAB variables using single quotes. For example, c = 'Hello'; The result is not a numeric matrix or.
C Characters and Strings
String Manipulation Chapter 7 Attaway MATLAB 4E.
ARRAY DIVISION Identity matrix Islamic University of Gaza
Text Manipulation Chapter 7 Attaway MATLAB 5E.
Presentation transcript:

Additional Data Types: 2-D Arrays, Logical Arrays, Strings Selim Aksoy Bilkent University Department of Computer Engineering

Summer 2004CS D Arrays a = [ 1:2:7; 10:2:16 ] a = [ x, y ] = size(a) x = 2 y = 4 a(2,:) ans = a(:) ans = Recall matrices and subarrays

Summer 2004CS D Arrays Adding the elements of a matrix function s = sum_elements(a) [r,c] = size(a); s = 0; for ii = 1:r, for jj = 1:c, s = s + a(ii,jj); end end

Summer 2004CS D Arrays a = [ 1:2:7; 10:2:16 ]; sum(a) ans = sum(a,1) ans = sum(a,2) ans = sum(a(:)) ans = 68 Adding the elements of a matrix (cont.)

Summer 2004CS D Arrays Finding the maximum value in each row function f = maxrow(a) [r,c] = size(a); f = zeros(r,1); for ii = 1:r, m = a(ii,1); for jj = 2:c, if ( m < a(ii,jj) ), m = a(ii,jj); end end f(ii) = m; end

Summer 2004CS D Arrays a = [ 1:2:7; 10:2:16 ]; max(a) ans = max(a,[],1) ans = max(a,[],2) ans = 7 16 max(a(:)) ans = 16 Finding the maximum value (cont.)

Summer 2004CS D Arrays Replace elements that are greater than t with the number t function b = replace_elements(a,t) [r,c] = size(a); b = a; for ii = 1:r, for jj = 1:c, if ( a(ii,jj) > t ), b(ii,jj) = t; end end end

Summer 2004CS 1118 Logical Arrays Created by relational and logical operators Can be used as masks for arithmetic operations A mask is an array that selects the elements of another array so that the operation is applied to the selected elements but not to the remaining elements

Summer 2004CS 1119 Logical Arrays Examples b = [ 1 2 3; 4 5 6; ] b = c = b > 5 c = whos Name Size Bytes Class a 2x4 64 double array b 3x3 72 double array c 3x3 72 double array (logical)

Summer 2004CS Logical Arrays Examples b(c) = sqrt( b(c) ) b = b(~c) = b(~c).^2 b =

Summer 2004CS Logical Arrays Examples c = b( ( b > 2 ) & ( b < 8 ) ) c = length( b( ( b > 2 ) & ( b < 8 ) ) ) ans = 5

Summer 2004CS Logical Arrays Examples [r,c] = find( ( b > 2 ) & ( b < 8 ) ) r = c =

Summer 2004CS Matrix Operations Scalar-matrix operations a = [ 1 2; 3 4 ] a = * a ans = a + 4 ans = Element-by-element operations a = [ 1 0; 2 1 ]; b = [ -1 2; 0 1 ]; a + b ans = a.* b ans =

Summer 2004CS Matrix Operations Matrix multiplication: C = A * B If A is a p-by-q matrix B is a q-by-r matrix then C will be a p-by-r matrix where

Summer 2004CS Matrix Operations Matrix multiplication: C = A * B

Summer 2004CS Matrix Operations Examples a = [ 1 2; 3 4 ] a = b = [ -1 3; 2 -1 ] b = a.* b ans = a * b ans =

Summer 2004CS Matrix Operations Examples a = [ 1 4 2; 5 7 3; ] a = b = [ 6 1; 2 5; 7 3 ] b = c = a * b c = d = b * a ??? Error using ==> * Inner matrix dimensions must agree.

Summer 2004CS Matrix Operations Identity matrix: I A * I = I * A = A Examples a = [ 1 4 2; 5 7 3; ]; I = eye(3) I = a * I ans =

Summer 2004CS Matrix Operations Inverse of a matrix: A -1 A A -1 = A -1 A = I Examples a = [ 1 4 2; 5 7 3; ]; b = inv(a) b = a * b ans =

Summer 2004CS Matrix Operations Matrix left division: C = A \ B Used to solve the matrix equation A X = B where X = A -1 B In MATLAB, you can write x = inv(a) * b x = a \ b (second version is recommended)

Summer 2004CS Matrix Operations Example: Solving a system of linear equations A = [ ; 2 8 2; ]; B = [ ]'; X = A \ B X =

Summer 2004CS Matrix Operations Matrix right division: C = A / B Used to solve the matrix equation X A = B where X = B A -1 In MATLAB, you can write x = b * inv(a) x = b / a (second version is recommended)

Summer 2004CS Strings A string is an array of characters s = 'abc' is equivalent to s = [ 'a' 'b' 'c' ] All operations that apply to vectors and arrays can be used together with strings as well s(1)  'a' s( [ 1 2 ] ) = 'XX'  s = 'XXc' s(end)  'c'

Summer 2004CS String Conversion Conversion of strings to numerical arrays double( 'abc xyz' ) ans = double( 'ABC XYZ' ) ans = Conversion of numerical arrays to strings char( [ ] ) ans = Hello!

Summer 2004CS Character Arrays 2-D character arrays s = [ 'my first string'; 'my second string' ] ??? Error s = char( 'my first string', 'my second string' ) s = my first string my second string size(s)  [ 2 16 ] size( deblank( s(1,:) ) )  [ 1 15 ] char function automatically pads strings

Summer 2004CS String Tests ischar() : returns 1 for a character array ischar ( 'CS 111' ) ans = 1 isletter() : returns 1 for letters of the alphabet isletter( 'CS 111' ) ans = isspace() : returns 1 for whitespace (blank, tab, new line) isspace( 'CS 111' ) ans =

Summer 2004CS String Comparison Comparing two characters 'a' < 'e' ans = 1 Comparing two strings character by character 'fate' == 'cake' ans = 'fate' > 'cake' ans =

Summer 2004CS String Comparison strcmp() : returns 1 if two strings are identical a = 'Bilkent'; strcmp( a, 'Bilkent' ) ans = 1 strcmp( 'Hello', 'hello' ) ans = 0 strcmpi() : returns 1 if two strings are identical ignoring case strcmpi( 'Hello', 'hello' ) ans = 1

Summer 2004CS String Case Conversion Lowercase-to-uppercase a = upper( 'This is test 1!' ) a = THIS IS TEST 1! Uppercase-to-lowercase a = lower( 'This is test 1!' ) a = this is test 1!

Summer 2004CS Searching in Strings findstr() : finds one string within another one test = 'This is a test!'; pos = findstr( test, 'is' ) pos = 3 6 pos = findstr( test, ' ' ) pos =

Summer 2004CS Searching in Strings strtok() : finds a token in a string [ token, remainder ] = strtok( 'This is a test!', ' ' ) token = This remainder = is a test! remainder = 'This is a test!'; while ( any( remainder ) ), [ word, remainder ] = strtok( remainder ); disp( word ); end

Summer 2004CS Replacing in Strings strrep() : replaces one string with another s1 = 'This is a good example'; s2 = strrep( s1, 'good', 'great' ) s2 = This is a great example

Summer 2004CS String Conversion Recall num2str() for numeric-to-string conversion str = [ 'Plot for x = ' num2str( 10.3 ) ] str = Plot for x = 10.3 str2num() : converts strings containing numbers to numeric form x = str2num( '3.1415' ) x =

Summer 2004CS String Conversion sprintf() is identical to fprintf() but output is a string str = sprintf( 'Plot for angle = %0.4f', pi ) str = Plot for angle =

Summer 2004CS String Comparison Example: Write a function that takes two strings and returns -1 if the first string is lexicographically less than the second 0 if they are equal +1 if the first string is lexicographically greater than the second Pseudocode: Get input strings Pad strings to equal length Compare characters from beginning to end and find the first difference Return a value depending on the first distance

Summer 2004CS String Comparison function result = c_strcmp(str1,str2) %C_STRCMP Compare strings like C function "strcmp" % Function C_STRCMP compares two strings, and returns % a -1 of str1 < str2, a 0 if str1 == str2, and a % +1 if str1 > str2. % Record of revisions: % Date Programmer Description of change % ==== ========== ===================== % 10/18/98 S. J. Chapman Original code % Check for a legal number of input arguments. msg = nargchk(2,2,nargin); error(msg); % Check to see if the arguments are strings if ~(isstr(str1) & isstr(str2)) error('Both str1 and str2 must both be strings!') else % Pad strings strings = strvcat(str1,str2); % Compare strings diff = strings(1,:) ~= strings(2,:); if sum(diff) == 0 % Strings match, so return a zero! result = 0; else % Find first difference between strings ival = find(diff); if strings(1,ival(1)) > strings(2,ival(1)) result = 1; else result = -1; end