Search code examples
matlabmatrixcopying

matlab efficient copying of matrix


I have matrix (a) with (1:10),<10 x 1> double. I would like to copy the values and rearrange them column wise into another matrix var. (b). See example below. Also, what method would be most efficient at this task?

matrix a    matrix b

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

update: Hi once again Amro. How about if I wanted to define which values to copy. See below example:

matrix a    matrix b
column:      1  2  3  4  5  6  7

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

Solution

  • Try:

    >> a = (1:10)'
    a =
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
    
    >> b = tril(repmat(a,1,10))
    b =
         1     0     0     0     0     0     0     0     0     0
         2     2     0     0     0     0     0     0     0     0
         3     3     3     0     0     0     0     0     0     0
         4     4     4     4     0     0     0     0     0     0
         5     5     5     5     5     0     0     0     0     0
         6     6     6     6     6     6     0     0     0     0
         7     7     7     7     7     7     7     0     0     0
         8     8     8     8     8     8     8     8     0     0
         9     9     9     9     9     9     9     9     9     0
        10    10    10    10    10    10    10    10    10    10