问题
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