【问题标题】:Appending string column to number column将字符串列附加到数字列
【发布时间】:2014-02-26 19:29:48
【问题描述】:

如何在 MATLAB 中将一列字符串附加到一列数字中?

例如,我有字符串列wrds 和数字列occurs

wrds={'the' 'of' 'to' 'and'}'; occurs=[103 89 55 20]';

我想将它们并排放置,以便它们显示如下:

'the' 103
'of'   89
'to'   55
'and'   20

你会认为这可以解决问题:

out={wrds occurs}

但是当我输入这个时我得到的输出是:

out =
{4x1 cell}    [4x1 double]

这什么也没告诉我。我怎样才能看到字符串和数字的实际显示?

【问题讨论】:

    标签: string matlab concatenation cell-array


    【解决方案1】:

    将数值数组转换为元胞数组并连接:

    >> out = [wrds(:) num2cell(occurs)]
    out = 
        'the'    [103]
        'of'     [ 89]
        'to'     [ 55]
        'and'    [ 20]
    

    作为num2cell 的更快替代方案,我建议sprintfcout = [wrds(:) sprintfc('%d',occurs(:))]

    【讨论】:

    • 现在我得到:out = {4x1 cell} {4x1 cell}
    • 啊,是的,我的错。谢谢。
    猜你喜欢
    • 2012-07-12
    • 2012-12-05
    • 2012-09-14
    • 2018-09-13
    • 2016-07-03
    • 2014-12-28
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多