问题
I am trying to compute the code seen below, in a more clever way, using MatLAB:
f=zeros(5,2) %Values irrelevant.
y = {'A1', 'B1';
f(1,1) f(1,2);
f(2,1) f(2,2);
f(3,1) f(3,2);
f(4,1) f(4,2);
f(5,1) f(5,2)};
What I get out is the f
matrix with the text of A1
and B1
above the two vectors.
I suppose there is a simpler way to write this, for more complex purposing, but I've tried any combination of parenthesis, brackets and curly brackets, num2str
that I could think of.
Any suggestions?
回答1:
The simplest solution is to use num2cell and concatenate the result with the strings:
y = [{'A1', 'B1'}; num2cell(f)];
Example
>> f = reshape(1:10, 2, [])';
>> y = [{'A1', 'B1'}; num2cell(f)]
y =
'A1' 'B1'
[ 1] [ 2]
[ 3] [ 4]
[ 5] [ 6]
[ 7] [ 8]
[ 9] [10]
来源:https://stackoverflow.com/questions/16174833/combining-a-matrix-of-numbers-with-array-of-strings