Copy a row or column of a matrix and insert it in the next row/column

三世轮回 提交于 2019-12-05 03:13:23

You can simply repeat the indices of the rows you'd like to repeat

A = A([1 1 2 3],:)

To insert row number source as row number target:

A = [A(1:target-1,:); A(source,:); A(target:end,:)];
A = [A(1,:); A];

I know this is a really old topic, but this post was coming up in searches I did for the same problem when I was looking for a specific Matlab function I couldn't remember the name of--padarray.

So, you could do:

A = [1 2 3; 4 5 6; 7 8 9];

A = padarray(A,[1 0],'replicate','pre');

This is often helpful if, for example, A is the output of a function that you have not saved explicitly, so you don't know what the first row is. Anyhow, hope this helps someone!

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