Combining a matrix of numbers, with array of strings

妖精的绣舞 提交于 2019-12-10 12:18:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!