将列添加到单元格数组 [英] Adding column to cell array

查看:135
本文介绍了将列添加到单元格数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a cell that has the following data:

Tom     Student
Jim     Faculty     
Clare   Student

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

What I want to do is add in another column in front to be a serial number.

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'

>> 

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

For numeric arrays in the first column instead:

>> 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'

您也可以使用num2cell(1:size(A,1))'代替上面的mat2cell,如Dan所指出的.

You can also use num2cell(1:size(A,1))' in place of mat2cell above, as noted by Dan.

这篇关于将列添加到单元格数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆