Insert a column of non-numeric data into a cell's column at arbitrary location and shift old data to the right

不打扰是莪最后的温柔 提交于 2019-12-12 02:25:48

问题


I need to insert a column (vector) with non-numeric data cells (one or multiple strings) into a cell of non-numeric data at an arbitrary location and retain old data columns by shifting them to the right.

my_word =

's'    'i'    'p'
's'    'i'    'p'
's'    'i'    'p'
's'    'i'    'p'
's'    'i'    'p'

my_column =

'a'
'b'
'c'
'd'
'e'

result_cell_1 =

's'    'a'    'i'    'p'
's'    'b'    'i'    'p'
's'    'c'    'i'    'p'
's'    'd'    'i'    'p'
's'    'e'    'i'    'p'

result_cell_2 =

's'    'i'    'a'    'p'
's'    'i'    'b'    'p'
's'    'i'    'c'    'p'
's'    'i'    'd'    'p'
's'    'i'    'e'    'p'

The major problem is that I want to insert a column at the second and the third location. In addition, I am not limited to the length in the example, so the solution should allow me to loop over a row of any length and insert a column at each location incrementally. Also, I have a solution for substitution each column, including the first and the last one, so they are not the problem. The problem is in the middle, where the length is not always the same.


回答1:


Manipulating cell arrays is very similar to normal matrices. If i were the column to insert,

result = [my_word(:,1:i-1) my_column my_word(:,i:end)]

would accomplish your goal. It breaks my_word into the first and second half, and concatenates them back together with my_column in the middle.



来源:https://stackoverflow.com/questions/33863776/insert-a-column-of-non-numeric-data-into-a-cells-column-at-arbitrary-location-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!