Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I was wondering if there is an easy way in MATLAB to do the following operation: I'd like to copy a row or column of a matrix and insert it in the next row/column.

For example: given a 3x3 matrix

1 2 3
4 5 6
7 8 9

I'd like to copy the first row and insert it as a second row:

1 2 3
1 2 3
4 5 6
7 8 9

Can someone advise how I could accomplish this in MATLAB? Thanks!

like image 663
freddy Avatar asked Jun 05 '11 23:06

freddy


People also ask

How do I copy rows from one row to another?

Move or copy just the contents of a cell You can also edit and select cell data in the formula bar. Select the row or column that you want to move or copy. In the cell, click where you want to paste the characters, or double-click another cell to move or copy the data. or press Ctrl+V.

How do you interchange rows and columns in a matrix?

Any 2 columns (or rows) of a matrix can be exchanged. If the ith and jth rows are exchanged, it is shown by Ri ↔ Rj and if the ith and jth columns are exchanged, it is shown by Ci ↔ Cj.


2 Answers

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

A = A([1 1 2 3],:)
like image 71
Jonas Avatar answered Oct 18 '22 21:10

Jonas


To insert row number source as row number target:

A = [A(1:target-1,:); A(source,:); A(target:end,:)];
like image 25
trutheality Avatar answered Oct 18 '22 20:10

trutheality