In: Computer Science
USING MATLAB
In signal processing applications, the Hankel matrix is useful. The elements of a (N × N) Hankel matrix are given by
h?? =0 ?+?−1>?
=?+?−1 otherwise
Write a script that takes as input from the user an integer, n. Your code should accept input ≥1 and <10. The user should be repeatedly prompted until acceptable input is entered. Next, create n Hankel matrices for N=1,2,...,n. That is, if the user enters 4, you’ll generate 1×1, 2×2, 3×3, and 4×4 Hankel matrices, then display them as shown below (on next page). The spacing doesn’t need to be identical to my answer, but try to get it as close as possible. Hint: Use for loops.
1x1 Hankel matrix:
1
2x2 Hankel matrix:
1 2
2 0
3x3 Hankel matrix:
1 2 3
2 0 0
3 0 0
4x4 Hankel matrix:
1 2 3 4
2 0 0 0
3 0 0 0
4 0 0 0
MATLAB Code:
% MATLAB Script that generates Hankell Matrix
clc;
clear;
% Reading N value
N = input("Enter N value: ");
% Loop till valid value is entered
while N<1 || N>=10
% Reading N value again
N = input("\nInvalid N Value!!! Re-Enter N value: ");
end % While end
% Creating N Hankell Matrics
for k=1:N
% Printing message
fprintf("\n%dx%d Hankel matrix:\n", k, k);
% Creating kxk Matrices
for i = 1:k
for j = 1:k
% Checking values
if (i+j-1) > k
fprintf("%-3d", 0);
else
fprintf("%-3d", (i+j-1));
end % If end
end % Inner for
fprintf("\n");
end % Outer for
end % Top for
____________________________________________________________________________________________
Sample Run: