我有一个包含以下数据的单元格:
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/