Is there a simpler or more efficient way to do the following?

1 view (last 30 days)
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+" ");
for columns = 2:columnsInB
% loop to add column numbers to the first row
B(1, columns) = columns - 1;

Accepted Answer

J. Alex Lee
J. Alex Lee on 6 Oct 2020
You could for example do this
function B = label1(A)
[rows,cols] = size(A);
B = ["",string(1:cols);[string((1:rows)')+char(9),A]];
I don't claim that it is simpler/more efficient, nor the most compact. Mixed feelings on whether it is a useful learning/teaching snippet either.
Likely the hw problem was written before "strings" were actually a thing, and the answer saught is a character array. At least that would be the more interesting hw problem to pose imho.
J. Alex Lee
J. Alex Lee on 6 Oct 2020
This line hides implicit type conversion and concatenation.
is shorthand for horizontal concatenation
is shorthand for vertical concatenation
Implicit type conversion with operations on strings.
3.1415 + ""
and you get the string
so you implicitly converted the double 3.1415 into a string as if you did
You already used the above version in your original code when converting A.
So you can create a columnar string array of row numbers with implicit conversion
and just concatenate with A, which also leads to implicit conversion of A into string(A).
C = horzcat(string((1:rows)')+char(9),A)
that's the "inner" concatenation with the square brackets.
Now you can make the header using the same principles
H = horzcat("",1:cols)
So you didn't even really need the string() around 1:cols.
or you can do it one shot using square brackets

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!