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

조회 수: 1 (최근 30일)
Terin Richardson
Terin Richardson 2020년 10월 6일
댓글: J. Alex Lee 2020년 10월 6일
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;
end
end

채택된 답변

J. Alex Lee
J. Alex Lee 2020년 10월 6일
You could for example do this
function B = label1(A)
[rows,cols] = size(A);
B = ["",string(1:cols);[string((1:rows)')+char(9),A]];
end
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.
  댓글 수: 3
Terin Richardson
Terin Richardson 2020년 10월 6일
For some reason my brain isn't wrapping around how you formatted the string to get the result. Can you explain the the third line and what it is doing exactly?
J. Alex Lee
J. Alex Lee 2020년 10월 6일
This line hides implicit type conversion and concatenation.
Concatenation:
[a,b]
is shorthand for horizontal concatenation
horzcat(a,b)
and
[c;d]
is shorthand for vertical concatenation
vertcat(c,d)
Implicit type conversion with operations on strings.
Try
3.1415 + ""
and you get the string
"3.1415"
so you implicitly converted the double 3.1415 into a string as if you did
string(3.1415)
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
string((1:rows)')+char(9)
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.
Then
B=vertcat(H,C)
or you can do it one shot using square brackets

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by