



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Some important Built-in function in MATLAB
d=[1 2 3;4 5 6] F=size(d) 2 3
cumsum(x) x=[2 4 5; 2 5 4; 4 5 6] cumsum(x) 2 6 11 2 7 11 4 9 15
cumprod(x) لنفس عملية الجمع التراكمي اعاله نعمل الضرب
x=[1 5 3] max(x) ans= x=[1 5 3;2 4 6] max(x) ans= 2 5 6 x=[1 5 3] [a,b]=max(x) a=5 value b=2index x=[1 5 3;2 4 6] y=[10 2 4;1 8 7] max(x,y) ans= 10 5 4 2 8 7
Ex6) write MATLAB program to print a table of 3 columns, where column 1 is the angles from 0 to 2π at a step of π/4, the second column is given by y=2(sin(x)+cos(x)) and the third column is the nearest integer of y.
disp & fprintf x=1:5; disp(‘the values are’) disp(x) عند التنفيذ يظهر على الشاشة The value are 1 2 3 4 5 لغرض اظهار الناتج بنفس الخط نستخدم امر الطبع ادناه disp([‘the values are,’num2str(x)]) the values are 1 2 3 4 5 num2str: to transfer the x matrix to character array. fprintf: formatted print function ex: feet=1: Inches=feet.*12; table=[feet;inches]; fprintf(‘%4.0 %7.2\n’,table) 1 12. 2 24. 3 36. %7. 7=no. of digit, 2= no. of decimal , \n =new line Ex9) you have weather data of 4 weeks of daily average temperatures (28values). Write matlab code to find and print the maximum, minimum and average temperature in each week. clear,clc t1=[20 21 24 22 25 21 23;… 21 21.5 28 23 25 22 23;… 25 24 24 23.2 25.5 23 26;… 22 21 29 27 25 23 22]; t=t1'; ma=max(t); mi=min(t); avg=mean(t); %disp(t)
%disp('----------------------------') %disp(ma) %disp(mi) %disp(avg) fprintf(%5.2f %5.2f %5.2f %5.2f\n',t1) frintf('---------------------------------------\n') r=[ma;mi;avg]; fprintf(%5.2f %5.2f %5.2f %5.2f\n',r) frintf('---------------------------------------\n') fprintf(%5.2f %5.2f %5.2f %5.2f\n',ma) fprintf(%5.2f %5.f2 %5.2f %5.2f\n',mi) fprintf(%5.2f %5.2f %5.2f %5.2f\n',avg)
Logical functions and control structures Sequence Programming structures Selection Repetition Relation operators < less than <= less than or equal to
Greater than = greater than or equal to == equal to ~= not equal to
x=5; y=1; ans= x<y 0 x=1:5; y=x-4; ans= x<y 0 0 0 0 0 x=[1 2 3 4 5]; y=[-2 0 2 4 6]; ans= x<y 0 0 0 0 1 Logical operator & and x=[0 2 3 4 0]; ~ not y=[-2 0 2 4 0];