matlab - 将列添加到元胞数组

标签 matlab

我有一个包含以下数据的单元格:

Tom     Student
Jim     Faculty     
Clare   Student

我想做的是在前面的另一列中添加一个序列号。

1   Tom     Student
2   Jim     Faculty     
3   Clare   Student

有人可以给点建议吗?

最佳答案

您将 A 定义为:

>> A={'Tom', 'Student'; 'Jim', 'Faculty'; 'Clare', 'Student'}

A = 

    'Tom'      'Student'
    'Jim'      'Faculty'
    'Clare'    'Student'

添加一列:

>> newCellCol = strsplit(num2str(1:size(A,1)))'

newCellCol = 

    '1'
    '2'
    '3'

>> A = [newCellCol A]

A = 

    '1'    'Tom'      'Student'
    '2'    'Jim'      'Faculty'
    '3'    'Clare'    'Student'

>> 

对于第一列中的数值数组:

>> newCellCol = mat2cell(1:size(A,1),1,ones(1,size(A,1)))';
>> A = [newCellCol A]

A = 

    [1]    'Tom'      'Student'
    [2]    'Jim'      'Faculty'
    [3]    'Clare'    'Student'

如 Dan 所述,您还可以使用 num2cell(1:size(A,1))' 代替上面的 mat2cell

关于matlab - 将列添加到元胞数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19674050/

相关文章:

matlab - 使用蒙特卡罗方法计算 n 球体积

返回代码大于零的 Matlab 退出力

matlab - 了解 Matlab stereoCameraCalibrator 上的外部立体参数(旋转)

matlab - 检查符号计算

algorithm - MATLAB 中超大矩阵的高效乘法

matlab - 在Matlab中的for循环中使用动态向量作为索引值

macos - 创建 Matlab 运行行快捷方式(如 R)

matlab - 制作具有多个 y 轴的条形图

matlab - MATLAB GUI 中的全局变量?

c - 错误: #error "must define number of sample times, NUMST"