Lecture 3
Lecture 3
built-in functions:
zeros()
function is used to create an array (or matrix) filled with zeros. You can specify
the dimensions of the array you want to create.
Ex: x = zeros(3),,,,, Create a 3x3 matrix of zeros
Z = zeros(2, 4),,,,, Create a 2x4 matrix of zeros:
……………………………………………………………………………………………………
ones()
function is used to create an array (or matrix) filled with ones. Similar to the
zeros() function, you can specify the dimensions of the array you want to create.
x = ones (3),,,,, Create a 3x3 matrix of ones
Z = ones (2, 4),,,,, Create a 2x4 matrix of ones:
……………………………………………………………………………………………….
size()
function is used to determine the dimensions of an array or matrix.
A = [1, 2, 3; 4, 5, 6];
dims = size(A)
B = [1, 2, 3, 4];
numRows = size(B, 1)
numCols = size(B, 2)
…………………………………………………………………………………………….
length()
function is used to determine the length of the largest dimension of an array or
vector.
v = [1, 2, 3, 4];
len = length(v)
M = [1, 2, 3; 4, 5, 6];
len = length(M)
……………………………………………………………………………………….
input()
function is used to prompt the user for input during the execution of a program
Syntax: variable = input(‘prompt’)
name = input('Enter your name: ', 's');
age = input('Enter your age: ');
…………………………………………………………………………………..
disp()
function is used to display a message or the value of a variable in the command
window. It provides a simple way to output information without formatting.
Always using string sometime you need to convert number to string
EX:
name = input('enter your name', 's')
age = input ('enter your age')
disp(['your name is: ', name])
disp(['your age is:', int2str(age)]),,, int2str(convert number into string datatype)
……………………………………………………………………………………………….
fprintf ()
function is used to display formatted text to the command window or to a file. It
provides more control over how output appears compared to the disp()
function, allowing you to specify formatting for different data types.
%d integer
%f floating point format
%e exponential format
\n new line character
\t tab character
EX:
fprintf('Hello,\n World!')
x=5
fprintf('The value of x is: %d', x)
piValue = pi;
fprintf('The value of pi is: %.2f', piValue)
name = 'Alice';
age = 30;
fprintf('Name: %s, Age: %d\n', name, age)
fprintf( 'Area of a circle with radius %d is %f', 3, pi*3^2 )
x= 5
fprintf( 'x = %6.2f', x );
x = 3.14
fprintf( 'x = %d\ny = %d\n', 3, 13 );
********************************************************
Other functions:
1- Abs (absolute value) …. abs(-1)
2- log10 (100)
3- max(4,7)
4- min(3,5)
5- round(2.23456789,3)
6- date
7- % (write any comment )