The assignment was to: "Write a program (function) that takes in an non-empty integer array A with unknown rows and columns and return an string matrix B with matrix A is padded at left hand side with row number followed by a tab character (char(9))and on top with column number"
The code I came up with and that seems to work is the following:
function B = label(A) %Function to label rows and columns in an array with numbers
B = string(A); % sets B to the string version of the A array
[~,columnsInB] = size(B); % gets the current size of our array
insertRow = zeros(1,columnsInB); % creates a placeholder to use later in inserting a row
rowToInsert = 1; % defines which row we will insert on
columnToInsert = 1; % defines which column we will insert on
B = [B(1:rowToInsert-1,:); insertRow; B(rowToInsert:end,:)]; % set the string array B to a copy of it which has the extra inserted
% row
[rowsInB,~] = size(B); % gets new size of array
insertColumn = zeros(rowsInB,1); % creates a placeholder to use later in inserting a column
B = [B(:,1:columnToInsert-1), insertColumn, B(:,columnToInsert:end)]; % set the string array B to a copy of it which has the extra inserted % column
[rowsInB,columnsInB] = size(B); % gets new size of array B(1) = ' '; % sets the first value to nothing
for rows = 2:rowsInB % loop to add row numbers to the first column with the indent
B(rows) = (rows - 1+" "); end for columns = 2:columnsInB % loop to add column numbers to the first row
B(1, columns) = columns - 1; endend
Best Answer