将一列非数字数据插入到单元格的任意位置的列中,并将旧数据向右移动 [英] Insert a column of non-numeric data into a cell's column at arbitrary location and shift old data to the right

查看:90
本文介绍了将一列非数字数据插入到单元格的任意位置的列中,并将旧数据向右移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将具有非数字数据单元格(一个或多个字符串)的列(向量)插入任意位置的非数字数据单元格中,并通过将其向右移动来保留旧数据列.

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 =

my_word =

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

my_column =

my_column =

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

result_cell_1 =

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 =

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.

推荐答案

操纵单元阵列与普通矩阵非常相似.如果i是要插入的列,

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)]

将实现您的目标.它将my_word分为上半部分和下半部分,并将它们与中间的my_column串联在一起.

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.

这篇关于将一列非数字数据插入到单元格的任意位置的列中,并将旧数据向右移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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