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
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
Post a Comment