Computing Tools For Mathematics(COMP1118) Topic: Arrays and Variables

Computing Tools For Mathematics(COMP1118)
Instructor : Engineer Imran Ahmad
University Of Okara (Renala Campus)

Lesson 4 :Arrays and Variables


Arrays:
The fundamental unit of data in any MATLAB program is the array.
An array is a collection of data values organizes into rows and column.
Array can be classified as either vector or matrices
π‘€π‘Žπ‘‘π‘Ÿπ‘–π‘₯             π‘Ž= [  1     2
                                  3     4
                                  5     6 ]

π‘…π‘œπ‘€ π‘£π‘’π‘π‘‘π‘œπ‘Ÿ     𝑏=[1      2       3      4]
πΆπ‘œπ‘™π‘’π‘šπ‘› π‘£π‘’π‘π‘‘π‘œπ‘Ÿ   𝑐=[ 2
                                   4
                                   5]
Variables
 A MATLAB variable: is a region of memory containing an array that is known by a user-specified name.
Variable name should start with a letter and then followed by an combination of letter, numbers and underscore(_) character.
Use meaningful name for the variable to make program easy to understand.
Space is not allowed in variable name.
 The MATLAB is case sensitive.
The most common types of MATLAB variables are double and char.
A variable type double is automatically created whenever a numerical value is assigned to a variable name.
When a character or string is assign to a variable name the type automatically declared as a char.
Multidimensional Arrays
 ≫π‘Ž=[1 2 3;4 5 6;7 8 9;10 11 12]
[1
 2
 3]  one dimentional array

 [1       2        3
 1        2        3
 1        2       3]  π‘€π‘’𝑙𝑑𝑖 π‘‘π‘–π‘šπ‘’π‘›π‘ π‘–π‘œπ‘›π‘Žπ‘™ π‘Žπ‘Ÿπ‘Ÿπ‘Žπ‘¦
No. of element in an array =  row*column
Sub Array
 π‘Žπ‘Ÿπ‘Ÿ1=[1 −2    3     −4    5]
 arr1(3)
π‘Žπ‘Ÿπ‘Ÿ1([1 4])
 π‘Žπ‘Ÿπ‘Ÿ1 (1:2:5)
π‘Žπ‘Ÿπ‘Ÿ2=[1 2 3;−2 −3 −4;3 4 5]
 π‘Žπ‘Ÿπ‘Ÿ2(1,:)
π‘Žπ‘Ÿπ‘Ÿ2 (:,1:2:3)
The end function
 Useful for creating array subscript.
 end returns the highest value taken on by that subscript.
 π‘Žπ‘Ÿπ‘Ÿ3=[1 2 3 4 5]
π‘Žπ‘Ÿπ‘Ÿ3(2:𝑒𝑛𝑑)
 π‘Žπ‘Ÿπ‘Ÿ4=[1 2 3 4;5 6 7 8;9 10 11 12]
 π‘Žπ‘Ÿπ‘Ÿ4(2:𝑒𝑛𝑑,2:𝑒𝑛𝑑)
Note:
 There is major difference between array and sub array in terms of values assignment.

 If values are assigned in a sub array, the selected values are updated.

 While in case of array the entire content of the array are deleted and replaced by the new values.

Initializing variables in MATLAB
 MATLAB variables are automatically created when they are initialized.
Three common ways to initialize a variable in MATLAB..
                Assign data to the variable in an assignment statement.
                Input data to the variable from the keywords
                Read data from a file.
             

Comments

Popular posts from this blog

Zong Whatsapp Packages 2019