%Clear any leftover variables and clear the command window clear, clc;
%Define the size of the big magic matrix A=3; %Define the column index that we will use as the centre square of the 3x3 matrix B=3; %Define the row index that we will use as the centre square of the 3x3 matrix C=3;
%If A<3, it is obviously impossible to get a 3x3 matrix from within it if A<3 disp('What is this, a matrix for ants?! Choose a larger magic sqaure size.'); else
%If B<=1 then part of the smaller matrix we want to extract lies outside of the larger one. if B<=1 disp('You chose an index for B that is too close to the edge or outside of the matrix. I have given you a 3x3 matrix as close to this edge as possible.'); B=2; end
%If B>=A then part of the smaller matrix we want to extract lies outside of the larger one. if B>=A disp('You chose an index for B that is too close to the edge or outside of the matrix. I have given you a 3x3 matrix as close to this edge as possible.'); B=A-1; end
%If C<=1 then part of the smaller matrix we want to extract lies outside of the larger one. if C<=1 disp('You chose an index for C that is too close to the edge or outside of the matrix. I have given you a 3x3 matrix as close to this edge as possible.'); C=2; end
%If C>=A then part of the smaller matrix we want to extract lies outside of the larger one. if C>=A disp('You chose an index for C that is too close to the edge or outside of the matrix. I have given you a 3x3 matrix as close to this edge as possible.'); C=A-1; end
%Create an AxA magic square matrix and print it to the screen bigMatrix=magic(A) %Select a 3x3 matrix within the AxA magic square matrix and print it to the screen smallMatrix=bigMatrix(B-1:B+1,C-1:C+1) end |
||