How to automatically create variables which are column extracts from a matrix

前端 未结 4 564
被撕碎了的回忆
被撕碎了的回忆 2021-01-20 11:34

I have an n*n matrix and I want to extract every 3 columns and keep the result in different variables.

I know that it is possible to do it in this way:

4条回答
  •  长发绾君心
    2021-01-20 11:58

    You really shouldn't split A this way. If you really want to adress A in 3 column block then use something like

    A = (1:9).*((1:9).');
    %% create anonymous function which can be called as Ac(1), Ac(2) and so on
    Ac = @(n) A(:,(n-1)*3+1:n*3)
    
    octave:2> Ac(1)
    ans =
    
        1    2    3
        2    4    6
        3    6    9
        4    8   12
        5   10   15
        6   12   18
        7   14   21
        8   16   24
        9   18   27
    
    octave:3> Ac(2)
    ans =
    
        4    5    6
        8   10   12
       12   15   18
       16   20   24
       20   25   30
       24   30   36
       28   35   42
       32   40   48
       36   45   54
    

提交回复
热议问题