Sunday, October 11, 2009

matlab basics : matrix num elements, row/column operations, lu decomposition




%num of matrix elements
numel(A)

%example of row/col operations for complete pivoting

B =

1 0 -2 1
2 2 2 0
2 2 4 0

%swop rows 1 and 3
>> B([1 3],:) = B([3 1],:)


B =

2 2 4 0
2 2 2 0
1 0 -2 1

%swop columns 1 and 3
>> B( :,[1 3]) = B(:,[3 1])

B =

4 2 2 0
2 2 2 0
-2 0 1 1

% it is now z y x

>> B(2,:) = (-1/2)*B(1,:) + B(2,:)

B =

4 2 2 0
0 1 1 0
-2 0 1 1

>> B(3,:) = (1/2)*B(1,:) + B(3,:)

B =

4 2 2 0
0 1 1 0
0 1 2 1


>> B([2 3],:) = B([3 2],:)

B =

4 2 2 0
0 1 2 1
0 1 1 0

>> B( :,[2 3]) = B(:,[3 2])

B =

4 2 2 0
0 2 1 1
0 1 1 0

%it is now z x y

>> B(3,:) = (-1/2) * B(2,:) + B(3,:)

B =

4.0000 2.0000 2.0000 0
0 2.0000 1.0000 1.0000
0 0 0.5000 -0.5000

>> rref(B)

ans =

1 0 0 0
0 1 0 1
0 0 1 -1

%x = 1
%y = -1
%z = 0



%lu decomposition

[L,U]=lu(some_matrix)

No comments:

Post a Comment