如何自动创建从矩阵中提取列的变量 [英] How to automatically create variables which are column extracts from a matrix

查看:72
本文介绍了如何自动创建从矩阵中提取列的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个n*n矩阵,我想每三列提取一次,并将结果保留在不同的变量中.

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:

A1 = A(:,1:3); 
A2 = A(:,4:6); 
A3 = A(:,7:9);

但是我想简化和自动化它来管理大量数据!

But I would like to simplify and automate this for managing a large amount of data!

A =

[1     2     3     4     5     6     7     8     9
 2     4     6     8    10    12    14    16    18
 3     6     9    12    15    18    21    24    27
 4     8    12    16    20    24    28    32    36
 5    10    15    20    25    30    35    40    45
 6    12    18    24    30    36    42    48    54
 7    14    21    28    35    42    49    56    63
 8    16    24    32    40    48    56    64    72
 9    18    27    36    45    54    63    72    81]

预期结果:

A1 =               
[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]

A2 =
[4  5   7 
 8  10  12
 12 15  18
 16 20  24 
 20 25  30
 24 30  36
 28 35  42 
 32 40  48
 36 45  54]

A3 =
[7  8   9 
 14 16  18
 21 24  27
 28 32  36 
 35 40  45
 42 48  54
 49 56  63  
 56 64  72 
 63 72  81]

推荐答案

您实际上不应该以这种方式拆分A.如果您真的想在3列的区块中查找A,请使用类似的

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

这篇关于如何自动创建从矩阵中提取列的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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