Programming by MATLAB 4. Updated...... - 121024214346
Programming by MATLAB 4. Updated...... - 121024214346
Programming by MATLAB 4. Updated...... - 121024214346
To call any element, its index in the matrix must be mentioned i.e mention its
row and column number inside a parentheses. For example, if you want to call the element
0.1000 from matrix A, you must type A(1,1).
1.2. Calling Elements of a MATRIX
• In the same way when calling element which lays in row 2 and column 3:
>> A(2,3)/A(2,2)
ans = ?
1.3. Adding Elements to a MATRIX
• Adding new element(s) to increase the size of matrix by adding entire row(s) or
column(s). For examples:
>> mat(5,5)=100 % adding new element located at 5th row and 5th column
1.3. Adding Elements to a MATRIX
>>mat(5,1:4)=[50 60 70 90] % assigns values to elements (1st to 4th column) of the 5th row
>> mat( : , 6)=111 % adds the 6th column with the same value for all its elements
1.3. Adding Elements to a MATRIX
>> mat (end , : ) = 200 % modifies all elements value of the last row (the 6th row)
>> mat( 2:4 , end)=50 % modifies the elements of (2nd to 4th row) of the last column
1.3. Adding Elements to a MATRIX
>> mat(2:4 , 4:end)=99
% modifies subset of matrix located at (2nd to 4th row) and (4th to last column)
with same value
1.3. Deleting Element(s) from a Matrix
• Deleting element(s) from matrix by deleting entire row(s) and entire column(s).
For examples:
>> mat( : , 4)=[ ] % deleting the 4th column
>> col_V= a( : , end) % assign the last column of matrix a to column vector
>> randi(10,5,3) % generates matrix (5 x 3) with random integer values range (1→10)
>> mat = size(r) % finds the size of matrix r and assigned it in row vector mat
ans =
0.1472 -0.1444 0.0639
-0.0611 0.0222 0.1056
-0.0194 0.1889 -0.1028
>> diag(a) % finds the diagonal elements of matrix a
ans =
8
5
2
>> det(a) % computes the determinant of matrix a(3 x 3)
ans =
-360
1.6. Matrix built-in Functions
Notes: If the argument of diag function is a vector, its output is a matrix (n x n) which n is the
length of the vector, the matrix’s main diagonal is the vector elements and the other
elements is zeros . For example:
>> v=[3 5 7 9]
v=
3 5 7 9