上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation 11

Vg101 Recitation I TAs
Vg101 Recitation I TAs

Agenda Vectors matrices Flow control Example1 (addGeneral) ·Plotting with Matlab ·Example2(regPloy)
• Vectors & matrices • Flow control • Example1 (addGeneral) • Plotting with Matlab • Example 2 (regPloy) Agenda

Operators (arithmetic) addition subtraction multiplication division power franspose ★ element-by-element mult element-by-element div element-by-element power
Operators (arithmetic) + addition - subtraction * multiplication / division ^ power ’ transpose .* element-by-element mult ./ element-by-element div .^ element-by-element power

Generating Vectors y a:step:b y linspace(a,b,n) y logspace(a,b,n) >>y logspace(1,2,6) y= 10.0000 15.8489 25.1189 39.8107 63.0957100.0000
Generating Vectors • y = a:step:b • y = linspace(a, b, n) • y = logspace(a, b, n)

Generating Matrices zeros(M,N)MxN matrix of zeros -ones(M,N) MxN matrix of ones ·rand(M,n) MxN matrix of uniformly distributed random numbers on (0,1) ·Direct define y=[123;456;789] y=[1,2,3:4,5,6,7,8,9]
Generating Matrices • zeros(M,N) MxN matrix of zeros • ones(M,N) MxN matrix of ones • rand(M,N) MxN matrix of uniformly distributed random numbers on (0,1) • Direct define y = [1 2 3;4 5 6;7 8 9] y = [1,2,3;4,5,6;7,8,9]

Matrices To delete a row or column 9>>A(3,:)=[] 之>A(:,1)=[] To define a sub-matrix >>B=A(L23].[12]) To concatenate matrices >>B=[A,10*A:-A,[100:010:001]
Matrices • To delete a row or column >> A(3, :) = [ ] >> A(:, 1) = [ ] • To define a sub-matrix >> B = A([2 3],[1 2]) • To concatenate matrices >> B = [A, 10*A; -A, [1 0 0; 0 1 0; 0 0 1]]

Flow Control if if...e nd if else ..end if ..elseif ..else ..end 。while.end loop ·for..end loop switch...end statement
Flow Control • if • – if ... end • – if ... else ... end • – if ... elseif ... else ... end • while…end loop • for...end loop • switch…end statement

Example 1 Write a function, c=addGeneral(a,b). which can add two matrices with different sizes together.Before adding,you should extend the matrices with zeros to the same size
Example 1 • Write a function, c=addGeneral(a,b), which can add two matrices with different sizes together. Before adding, you should extend the matrices with zeros to the same size

Sample input Sample output a=[123:456;789] a= >>c=addGeneral(a,b) 3 6 2 4 3 9 7 9 6 >b=[12;34] 7 8 9 b 1 2 3 4
Sample input >> a=[1 2 3;4 5 6;7 8 9] a = 1 2 3 4 5 6 7 8 9 >> b = [1 2;3 4] b = 1 2 3 4 Sample output >> c = addGeneral(a,b) c = 2 4 3 7 9 6 7 8 9

function c addGeneral(a,b) [row a,col a]size(a); [row b,col b]size(b); if row a row b if col a col b b [b,zeros(row b,col a col b)]; else a [a,zeros(row a,col b col a)]; end b [b;zeros (row_a row _b,max(col_a,col_b))]; else if col a col b b [b,zeros (row b,col a col b)]; else a [a,zeros(row a,col b col a)]; end a [a;zeros(row b -row a,max(col a,col b))]; end c=a+b;
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Other Resources_intrduction to computer and programming with C++ and MATLAB.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Assignments_assignment13.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Assignments_assignment12.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Assignments_assignment11.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Assignments_assignment 14.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Assignments_assignment 10_1.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Assignments_assignment 10.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C语言_VIII.结构体与共用体.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C语言_VII.指针.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C语言_VI.编译预处理.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C语言_V.数组.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C语言_IX.文件.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C语言_IV.函数和程序结构.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C语言_III.流程控制.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C语言_II.基本数据类型和运算.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C语言_I.C语言概述.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C++语言_VII.类与对象的高级操作.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C++语言_VI.类与对象.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C++语言_V. 流库.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)C++语言_IV.异常处理与模板.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_recitation 14.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Sample Exam Questions_midterm 2 sample solution.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Sample Exam Questions_Midterm 2008 sample.pdf
- 上海交通大学:《程序设计基础》课程教学资源(PPT课件讲稿)第1-2章 计算机简介、C++编程入门.ppt
- 上海交通大学:《程序设计基础》课程教学资源(PPT课件讲稿)第4-5章 控制结构.ppt
- 上海交通大学:《程序设计基础》课程教学资源(PPT课件讲稿)第6章 过程封装——函数.ppt
- 上海交通大学:《程序设计基础》课程教学资源(PPT课件讲稿)第7章 数组.ppt
- 上海交通大学:《程序设计基础》课程教学资源(PPT课件讲稿)第8章 间接访问——指针.ppt
- 上海交通大学:《程序设计基础》课程教学资源(PPT课件讲稿)第9章 数据封装——结构体.ppt
- 上海交通大学:《计算机硬件技术》课程PPT教学课件(计算机概论,孙德文).ppt
- 上海交通大学:《理论力学》课程教学资源(PPT讲稿)平面矢量.ppt
- 中华人民共和国国家标准(GB/T8567一2006 代替GB/T8567-1988)计算机软件文档编制规范 Specification for computer software documentation.pdf
- 《软件工程概论》课程教学资源(参考资料)Guide to the Software Engineering Body of Knowledge(SWEBOK)Version 3.0.pdf
- 《软件工程概论》课程教学资源(参考资料)软件工程知识体系指南(2004版).pdf
- 上海交通大学:《软件测试与质量控制》课程教学资源(课件讲稿)第1章 Introduction of software testing.pdf
- 上海交通大学:《软件测试与质量控制》课程教学资源(课件讲稿)第3章 Test environment and tools.pdf
- 上海交通大学:《软件测试与质量控制》课程教学资源(课件讲稿)第2章 Prerequisites Mathematics knowledge of software test.pdf
- 上海交通大学:《软件测试与质量控制》课程教学资源(课件讲稿)第4章 Fundamental theory and methods of software test.pdf
- 上海交通大学:《软件测试与质量控制》课程教学资源(课件讲稿)Chapter 2 fundamentals of software test.pdf
- 上海交通大学:《软件测试与质量控制》课程教学资源(课件讲稿)chapter 1 Introduction of software testing.pdf