How to Access Variable in MATLAB

How to access variable in Matlab?By default, MATLAB identify variable which you use as a matrix or array. So for variable which contain more than one element, addressing each element variable element in MATLAB following the following notation:
variable(line, column)

For example you can define in command window a matrix which has 3x3 dimension:
>> a = [1 3 5; 5 6 7; 8 2 4]
a =
     1 3 5
     5 6 7
     8 2 4

To access single element of matrix above as follow :
>> a(3,2)
ans =
         2

It means that you are accessing matrix element of 'a' variable in third line and second column.

To access element in the certain line as follow:
>> a(3,:)
ans =
         8 2 4
It means that you are accessing matrix elements of 'a' variable in third line. Sign double dot (:) at the matrix column means all column.

To access element in the certain column as follow:
>> a(:,3)
ans =
         5
         7
         4

It means that you are accessing matrix element of 'a' variable in the third column. Sign double dot (:) in the line means all line

To access some elements in certain line and column as follow:
>> a(1:2,2:3)
ans =
        3 5
        6 7

It means that you are accessing matrix element of 'a' variable in first to second line and in second to third column. Writing (1:2) at the line means first to second line. Writing (2:3) at the column means second to third column.

0 comments:

Post a Comment