【问题标题】:How to convert a vector to a cell array in matlab?如何在matlab中将向量转换为元胞数组?
【发布时间】:2015-07-20 02:46:47
【问题描述】:

我有如下两段代码:

nodes = randsample ( n_nodes, round(j*n_nodes) )-1;      
nodes_seqs = arrayfun (@transfer, nodes, 'UniformOutput', false );

nodes = num2str(nodes);

nodes = cellstr(nodes);
file_n = strcat('fasta','_','myfilename' );  % file  name

fastawrite(strcat( file_n, '.fas' ), nodes, nodes_seqs);

另一个是:

nodes = 0 : n_nodes-1;

nodes_seqs = arrayfun (@transfer, nodes, 'UniformOutput', false );

nodes = num2str(nodes);

nodes = cellstr(nodes);
file_n = strcat('myfilename' );  % file  name

fastawrite(strcat( file_n, '.fas' ), nodes, nodes_seqs);

第一个按预期运行。但是,我收到第二个错误。检查变量后,我在第二个变量中得到,节点是 1 × 1 单元格数组。我很困惑。为什么它适用于第一个,而不是第二个?非常感谢您的时间和关注。

【问题讨论】:

  • 感谢您的关注。终于知道原因了。

标签: matlab data-structures


【解决方案1】:

在第二种情况下,因为节点是您需要使用的元胞数组

nodes = cell2mat(nodes) 而不是 num2str,因为您的数据此时属于 cell 类型,而不是 num。

这里是函数documentation on Mathworks的链接

其他选项为函数cell2struct()cell2table()

【讨论】:

    【解决方案2】:

    我想通了。这里的主要原因是:在第一个片段中,randsample 返回一个列向量,而在第二个片段中,节点被初始化为一个行向量。添加转置后,它可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-06
      • 1970-01-01
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多