W1005 Intro to CS and Programming in MATLAB. Data Structures. Fall 2014 Instructor: Ilia Vovsha. hep://

Similar documents
COMS 3101 Programming Languages: MATLAB. Lecture 3

COMS 3101 Programming Languages: MATLAB. Lecture 4

COMS 3101 Programming Languages: MATLAB. Lecture 2

Matlab course at. P. Ciuciu 1,2. 1: CEA/NeuroSpin/LNAO 2: IFR49

Introduction to MATLAB for Engineers, Third Edition

Spring 2010 Instructor: Michele Merler.

Chapter 6 User-Defined Functions. dr.dcd.h CS 101 /SJC 5th Edition 1

COMS 3101 Programming Languages: MATLAB. Lecture 5

Introduction to MATLAB 7 for Engineers

Physics 326G Winter Class 2. In this class you will learn how to define and work with arrays or vectors.

W1005 Intro to CS and Programming in MATLAB. MATLAB Basics. Fall 2014 Instructor: Ilia Vovsha. hcp://

ECE 102 Engineering Computation

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

Topic 8. String Manipulation

Variable names are an example of an identifier name: The name must begin with a letter of the alphabet Using mixed case can solve some problems with

MATLAB for Engineers

String Manipulation. Chapter 7. Attaway MATLAB 4E

Previous Lecture: Today s Lecture: Announcement: Working with images. Characters and strings. Project 4 due Mon 10/26 at 11pm

Introduction to MATLAB

Computer Programming: C++

Today s topics. Announcements/Reminders: Characters and strings Review of topics for Test 1

Data Structures: Cell Arrays and Structures. Chapter 8. Linguaggio Programmazione Matlab-Simulink (2017/2018)

MATLAB. MATLAB Review. MATLAB Basics: Variables. MATLAB Basics: Variables. MATLAB Basics: Subarrays. MATLAB Basics: Subarrays

Computational Methods of Scientific Programming

Engineering Problem Solving with C++, Etter

CME 192: Introduction to Matlab

MAT 343 Laboratory 2 Solving systems in MATLAB and simple programming

MATH36032 Problem Solving by Computer. More Data Structure

C Characters and Strings

Introduction. Like other programming languages, MATLAB has means for modifying the flow of a program

Information for Candidates. Test Format

Intro Chapter 1 Demo Help Lookfor Doc Quit exit Variable variablename = expression = assignment operator, initializing Incrementing Identifier names

Lecture 3: Array Applications, Cells, Structures & Script Files

Dr. Iyad Jafar. Adapted from the publisher slides

Lecture 3: Array Applications, Cells, Structures & Script Files

10 M-File Programming

function [s p] = sumprod (f, g)

Introduction to. The Help System. Variable and Memory Management. Matrices Generation. Interactive Calculations. Vectors and Matrices

CS 303E Fall 2011 Exam 2 Solutions and Criteria November 2, Good Luck!

MATLAB: The Basics. Dmitry Adamskiy 9 November 2011

Beyond the Mouse A Short Course on Programming

Introduction to MATLAB

Introduction to Matlab. By: Hossein Hamooni Fall 2014

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

Programming in C. Session 7. Seema Sirpal Delhi University Computer Centre

today cs3157-fall2002-sklar-lect05 1

1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 169 / 221

Intermediate Programming, Spring 2017*

MATLAB -Specific Techniques

LAB 2 VECTORS AND MATRICES

INTRODUCTION TO MATLAB

Exercise 1.1 Hello world

Signal and Systems. Matlab GUI based analysis. XpertSolver.com

Write a C program using arrays and structure

Introduction to MATLAB

EE 355 Lab 4 - Party Like A Char Star

Computers Programming Course 10. Iulian Năstac

NatSciLab - Numerical Software Introduction to MATLAB

EN 001-4: Introduction to Computational Design. Matrices & vectors. Why do we care about vectors? What is a matrix and a vector?

Introduction to string

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

Computers Programming Course 11. Iulian Năstac

Homeworks on FFT Instr. and Meas. for Communication Systems- Gianfranco Miele. Name Surname

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

Statement of integrity: I did not, and will not, violate the rules of academic integrity on this exam.

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

COMP1917: 09 Arrays and Strings

Computer Science & Engineering 150A Problem Solving Using Computers

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

LECTURE 1. What Is Matlab? Matlab Windows. Help

Unit 2: The string class

AP Computer Science. Strings. Credit: Slides are modified with permission from Barry Wittman at Elizabethtown College

Do not start the test until instructed to do so!

SECTION 2: PROGRAMMING WITH MATLAB. MAE 4020/5020 Numerical Methods with MATLAB

CS1 Lecture 11 Feb. 9, 2018

Full file at

Lecture 15 MATLAB II: Conditional Statements and Arrays

QUIZ: What is the output of this MATLAB code? >> A = [2,4,10,13;16,3,7,18; 8,4,9,25;3,12,15,17]; >> length(a) >> size(a) >> B = A(2:3, 1:3) >> B(5)

Introduction To MATLAB Interactive Graphics

INTRODUCTION TO MATLAB Part2 - Programming UNIVERSITY OF SHEFFIELD. July 2018

MATLAB for the Sciences

CS 261 Fall Mike Lam, Professor. Structs and I/O

'C' Programming Language

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

COMS 3101 Programming Languages: MATLAB. Lecture 6

CS 230 Programming Languages

1 Introduction to MATLAB

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Fundamental of Programming (C)

Lectures 5-6: Introduction to C

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

CS 33. Introduction to C. Part 5. CS33 Intro to Computer Systems V 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

CSE374 Winter 2016, Midterm Examination February 8, 2016 Please do not turn the page until the bell rings.

Lecture 12. We have already used strings. Strings. Hello Class is a string.

Lecture 2: Advanced Programming Topics

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

% Edit the above text to modify the response to help Video_Player. % Last Modified by GUIDE v May :38:12

Transcription:

W1005 Intro to CS and Programming in MATLAB Data Structures Fall 2014 Instructor: Ilia Vovsha hep://www.cs.columbia.edu/~vovsha/w1005

Outline Cell arrays FuncNons with variable arguments Structure arrays (Structs) 2

Cell Arrays (purpose) Group different data types into a single container MathemaNcal operanons are not defined on the container Must address content of each cell Must idennfy number of cell Each element (cell) of a cell array can hold any data type ManipulaNon of cell arrays is similar to arrays 3

Cell Arrays (ininalizanon) IniNalizaNon: C = cell(n); % N by N array of empty cells C = cell(n,m); % N by M array of empty cells Use brackets {} to access content of cells, () to index C = {[1] [1,2,3]; rand(3), rand(3)}; A(1,1) = { [1,2,3] }; A{1,1} = [1,2,3]; celldisp(a); % Create directly % add element % add element % display all contents 4

Cell Arrays (manipulanon) Similar to arrays: A = cell(3); B = cell(3); C(3,:) = []; D = C([1,2],:) [r,c] = size(a); iscell(b) [a,b,c] = deal(a{1,:}); % 3 by 3 arrays of empty cells % access % access % size % check % deal contents to variables 5

num2cell() Problem 1: create a vector V of length N, where each element, V(k) = 2^k Problem 2: create a cell array C of length N, where each element, C{k} = 2^k SoluNon (1): 1. idx = 1:N; 2. V = 2.^idx; % The dot is required here SoluNon (2): Cell array, solunon (1) doesn t work! Instead of using a for- loop could convert numeric array to cell 6

num2cell() Problem 2: create a cell array C of length N, where each element, C{k} = 2^k SoluNon (2): 1. idx = 1:N; 2. V = 2.^idx; % V is a numeric array 3. C = num2cell(v); % Converted V to a cell array C 7

cellfun() Apply certain funcnons to all cells: Example: 1. cellfun( <funcnon>, < cell array>); % generic form 2. cellfun( isempty, A); % returns logical matrix 3. cellfun( length, A); % returns length 8

FuncNons (variable arguments) FuncNon which can have any number of input args Rules: Keyword: varargin varargin is a cell array containing the opnonal arguments ONLY Must be declared as the last input argument Must be lowercase Example: funcnon my_fun(x,y,z,varargin) Similarly, use varargout to create a funcnon with any number of output args. The same rules apply (last, lowercase) funcnon [ <out_var1>, varargout] = <name> ( <var1>,<var2>, varargin) varargin, varargout are cell arrays, ith cell is the ith argument nargin, nargout return the actual number of arguments used 9

Exercise (in class) Variable input arguments Write a funcnon with at least 3 input parameters {A, B, v1, v2, v3 }, which applies the funcnons stored in the strings v1 (v2, v3 if present) to the variables {A,B} which can be either cell arrays or matrices (you need to account for both opnons). 10

Structure Arrays (ininalizanon) Structs are similar in purpose to cell arrays MulNple fields, different data structures for each field Use dot to access fields Must idennfy name (not number) of element IniNalizaNon: S = struct([]) % Empty struct, no fields S = struct ( f1, v1, f2, v2) S.f1 = 2.5; % Struct with two fields % Create directly 11

Structs (get content) Add element: S.f1 = 2.5; S.f2 = hi ; % Create directly S(2).f1 = 3; % Add element S(1).f1 = 1.5; % Access changed! S(3).f3 = rand(3); % Add field S(:).f1 % get field values S(2:3).f3 % get field values 12

Structs (funcnonality) Useful funcnons: isstruct(s); S = sexield(s, field1, 5); isfield(s, field1 ); fieldnames(s) strings Example: % Check if S is a struct % Set a field to a value % Check if field exists % return cell array of 1. S = struct( vec, [1,2,3], mat, rand(3)); 2. my_field = mat ; 3. isfield(s, my_field); 13

Cell to Struct Conversion Commands: cell2struct(), struct2cell() S = cell2struct(c, my_fields, my_dim) my_fields is a cell array of strings Example: 1. my_fd = { num, name, nanon }; 2. my_arr = {8, Iniesta, Spain }; 3. my_st = cell2struct(my_arr, my_fd, 2); % You are folding the dimension, size must match 14

Strings (representanon) Strings are a sequence of characters, hence numerical arrays of ASCII values FuncNons for manipulanng arrays apply: t = Hello World! ; size(t), double(t), char(double(t)) Similar to arrays, must have equal # of columns, hence blanks must be padded: v = char( a, ab, abc ); v = strvcat( a,, ab, abc ); strcat(), deblank() 15

Strings (funcnons) MATLAB is not recommended as a tool for manipulanng strings. However, the funcnonality is available Check string: isleeer(str), isspace(str) Convert string: lower(str), upper(str) str2num(str), num2str(num) 16

Strings (examples) OperaNons on strings: strtok(str, delim), strcmp(str1,str2), strfind(str1,paeern) Regular expressions (help regexp): concise and flexible means for matching strings Example: 1. str1 = one!, no two ; 2. [bef_delim, a _delim] = strtok(str1,! ); 3. idx = strfind(str1, n ); 4. res1 = strcmp( hi, HI ); 5. res2 = strcmp( hi, lower( HI ); 17