Download Built-in Functions in MATLAB: A Comprehensive List and more Study Guides, Projects, Research Matlab skills in PDF only on Docsity! 11 Some important Built-in function in MATLAB 1. Square root b=sqrt(x) b=sqrt(4) 2 2. Remainder of dividing x/y a=rem(x,y) a=rem(10,3) 1 3. Number of 1D arrayed n=length(d) d=[3 2 -1 7] Length(d) 4 4. Number of rows & columns of a 2D array [row,cols]=size(d) F=size(d) d=[1 2 3;4 5 6] F=size(d) 2 3 5. Absolute value abs(x) 6. Any root of x nthroot(x,n) nthroot(8,3) 2 7. The sign of x sign(x) -1 or 1 8. Summation sum(x) x=[1,5,3]; y=[1,5,3;2,4,6]; disp(sum(x)) disp(sum(y)) 9 3 9 9 9. Product prod(x) prod([1 5 3]) 15 prod(y) 2 20 18 10. Cumulative Summation cumsum(x) x=[2 4 5; 2 5 4; 4 5 6] cumsum(x) 2 6 11 2 7 11 4 9 15 11. Cumulative Product cumprod(x) لنفس عملية الجمع التراكمي اعاله نعمل الضرب 12. Average mean(x) x=[1 5 3]; mean(x) 3 y=[1 2 3;4 5 6]; mean(y) 2.5 3.5 4.5 13. Ascending sorting sort(x) تصاعدي يطبق على كل عمود 11 14. Descending sorting sort(x,'descend') يطبق على كل عمود تنازلي 15. sortrows(x) الفرز على اساس العمود االول تصاعدي 16. sortrows(x,n) الفرز على اساس العمود المختار فقط تصاعدي 17. nearest integer round(x) round(8.6) ans=9 18. Truncation fix(x) fix(8.6) fix(-8.6) ans=8 ans=-8 19. Towards the lesser floor(x) floor(-8.6) ans=-9 20. Towards the greater ceil(x) ceil(-8.6) ans=-8 21. Maximum max(x) [a,b]=max(x) max(x,y) x=[1 5 3] max(x) ans=5 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 22. Minimum min(x) [a,b]=min(x) min(x,y) 23. factorial(x) 5!=5*4*3*2*1 factorial(5) ans=120 24. exp(x) 25. log(x) 26. log10(x) 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. 11 %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]; 11 | or a=xor(x,y) xor exclusive or a= 1 1 0 0 0 x=[1 2 3 4 5]; y=[-2 0 2 4 6]; z=[8 8 8 8 8]; z>x &z>y ans= 1 1 1 1 1 x>y|x>z ans= 1 1 1 0 0 Find : The (find) command searches a matrix and identifies which elements in that matrix meet a given criterion. Ex10) write matlab code to identify the accepted candidates to military service which are 1.8m tall or taller. (take 8 candidates) Sol. clear,clc height=[1.75 1.85 1.8 1.82 1.78 1.72 1.81 1.84]; accept=find(height>=1.8); disp('the following candidates meet the height required') fprintf('candidate No. %2.0f is %4.2f meterall\n',[accept;height(accept)])