Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Team:

1. Marisnelvys Cabreja Consuegra


2. Dannys García Miranda
Answers
Practice 21
%1)
A =[1/3 1/3 2/3; 2/3 2/3 4/3; 1/3 2/3 3/3; 2/5 2/5 4/5; 3/5 1/5 4/5]
rank1=rank(A,10^(-16))% the rank of A is 3, tolerance of 1e-16.

Since all the elements of this matrix are nonzero, the simplest way to interpret its
range is to consider the dimension of the largest nonzero square submatrix, which in
this case is 3 (or the number of rows or columns that are linearly independent), or
in other words, the range is the number of rows or columns that has the largest non-
zero square submatrix that we can form, being this time 3x3.

%2)
[U,S,V] = svd(A);
X=U*S*V'% it is true that X=A

Svd computes the singular values of a full rank matrix, like is the case with A,
of svd the range is calculated using the number of nonzero singular values, of the
diagonal of S which in this case coincides with 3, therefore reaffirms that A has
full range 3.

%3)
rank2= rank(A) % the rank here is 2

In %3 the rank is 2 because the matrix is not considered to be full rank, since the
default algorithm calculates the number of singular values larger than
max(size(A))*eps(norm(A)). For this matrix, the small value on the diagonal is
excluded since it is smaller than the tolerance. Tolerance, specified as a scalar,
in %1, Tol=10^(-16) and S(3,3)= 1.2019e-16, Tol>S(3,3).
Practice 22
%1)
n=90;
q=1.2;
s=sin(q);
c=cos(q);
D=zeros(n);D(1,1)=1; %Diagonal Matrix
C=zeros(n);C(:,:)=-c;C=triu(C,1); % Upper triangular Matrix
for i=1:n-1
C(i,i)=1;C(n,n)=1;
D(i+1,i+1)=s^i;
end
A1=D*C; %% Kahan Matrix
max=max(diag(A)) %is obtained as a result 1
min=min(diag(A)) %is obtained as a result 0.0019

%2)
A=gallery('kahan',90,1.2,0);
sig=svd(A);
rankA= rank(A) % the rank here is 89
e1=sig(1) % the value of sig1= 8.7893
e89= sig(89) % the value of sig89= 0.0024
e90= sig(90) % the value of sig90= 3.9607e-15

% svd returns the singular values of matrix A in descending order, The rank gives
a measure of the dimension of the range or column space of the matrix, which is the
collection of all linear combinations of the columns. e1>>e90, the first termo f svd
is the maximun an the last is the minimun.A matrix is full rank if its rank is the
highest possible for a matrix of the same size, and rank deficient if it does not
have full rank. In this case, the rank is 89 and there exist 90 values nonzeros, so
the calculation of the range is not considering e90.

%3)
A=gallery('kahan',90,1.2,2.25);
help private/kahan
% kahan Kahan matrix.
B = GALLERY('kahan', N, THETA, PERT) is an upper trapezoidal
matrix that has interesting properties regarding estimation of
condition and rank. If N is a two-element vector, then B is
N(1)-by-N(2); otherwise, B is is N-by-N. The useful range of
THETA is 0 < THETA < PI. THETA defaults to 1.2.

To ensure that the QR factorization with column pivoting does not


interchange columns in the presence of rounding errors, the
diagonal is perturbed by PERT*EPS*DIAG( [N:-1:1] ). The default
PERT is 1E3, which ensures no interchanges for GALLERY('kahan',N)
up to at least N = 100 in IEEE double precision arithmetic.
sig=svd(A);
rankA= rank(A) % the rank here is 89
e1=sig(1) % the value of sig1= 8.7893
e89= sig(89) % the value of sig89= 0.0024
e90= sig(90) % the value of sig90= 3.9606e-15 it is the unique diference with %2,
that was 3.9607e-15.
[Q,R,E]=qr(A);
dif=norm(eye(90)-E) % it is 0, therefore it is checked that E if is the identity
matrix
R90=R(90, 90) % The value obtained is 0.0019

The QR code can not correct the deficiency of range, because the minimum SVD is
3.9606E-15, very small, so the rank code discards it, there is no permutation
Practice 23
%1
load beach.jpg;
X = imread('beach.jpg'); % load image ‘beach’
X=X(1:512,1:512,:);% The matrix X is 801x804x3, 3D uint8, so in power series of 2
is 2^9=512 the most approximate to 801

colormap(pink(255))
figure(1); subplot(1,1,1); imshow(mat2gray(X)); title('Original image')
axis square

%2
[cr,bpp] = wcompress('c',X,'beach.wtc','spiht','maxloop',10);%p=10,cr=compression
ratio and bpp=bit-per-pixel ratio
Xc = wcompress('u','beach.wtc');
delete('beach.wtc')
colormap(pink(255))
figure(2); subplot(2,2,1); imshow(mat2gray(Xc)); title('Compressed image p=10')
axis square

[cr,bpp] = wcompress('c',X,'beach.wtc','spiht','maxloop',100);%p=100
Xc = wcompress('u','beach.wtc');
delete('beach.wtc')
colormap(pink(255))
figure(2); subplot(2,2,2); imshow(mat2gray(Xc)); title('Compressed image p=100')
axis square

[cr,bpp] = wcompress('c',X,'beach.wtc','spiht','maxloop',200);%p=200
Xc = wcompress('u','beach.wtc');
delete('beach.wtc')
colormap(pink(255))
figure(2); subplot(2,2,3); imshow(mat2gray(Xc)); title('Compressed image p=200')
axis square

[cr,bpp] = wcompress('c',X,'beach.wtc','spiht','maxloop',400);%p=400
Xc = wcompress('u','beach.wtc');
delete('beach.wtc')
colormap(pink(255))
figure(2); subplot(2,2,4); imshow(mat2gray(Xc)); title('Compressed image p=400')
axis square

%3
% We find matrix size for original matrix and we have:
[a,b,c]=size(X);% original matrix size, 801x804x3
Round we have that 801x801 and for to give the rate of compression of each picture
for p=10 we have 801*10/801: 801*10/801=10:10
for p=100 we have 801*100/801: 801*100/801=100:100
for p=200 we have 801*200/801: 801*200/801=200:200
for p=400 we have 801*400/801: 801*400/801=400:400
and p*(resolution(axa))+p=cr

Other method (SVD)

X=imread('beach.jpg');% If you want to view the original image


figure(1);
imshow(mat2gray(X)); % If you want to view the original image
title('Original Image')

p=[1,10,100, 200];%set different ranks


X=double(X); %to know the rank of each matrix, returns an array
%split into three channels
rr=rank(X(:,:,1));%source_red, In order to know the rank of each matrix, with
k=1,2,3, depending on the color
rg=rank(X(:,:,2));%source_blue, k=2, color green.
rb=rank(X(:,:,3));%source_green, k=3, color blue.
[sr vr dr]=svd(X(:,:,1));
[sg vg dg]=svd(X(:,:,2));
[sb vb db]=svd(X(:,:,3));

a=0;
for i=1:length(p)
%re=s*v*d'; Reconstruction
rer=sr(:,:)*vr(:,1:p(i))*dr(:,1:p(i))'; % Reconstruction (red)
reg=sg(:,:)*vg(:,1:p(i))*dg(:,1:p(i))'; % Reconstruction (green)
reb=sb(:,:)*vb(:,1:p(i))*db(:,1:p(i))'; % Reconstruction (blue)
re(:,:,1)=rer;
re(:,:,2)=reg;
re(:,:,3)=reb;
figure(2);
a=a+1
subplot(2,2,a)
imshow(mat2gray(re));
title(['Image for Rank=' num2str(p(i))])
end

You might also like