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.
Best Answer