当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

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

资源类别:文库,文档格式:PDF,文档页数:20,文件大小:144.01KB,团购合买
点击下载完整版文档(PDF)

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;

点击下载完整版文档(PDF)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共20页,试读已结束,阅读完整版请下载
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有