形成了从小型矩阵的N个大矩阵 [英] Form a large matrix from n numbers of small matrices

查看:210
本文介绍了形成了从小型矩阵的N个大矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的MATLAB。我已经生成的 N 数较小的矩阵,比如 3×1 使用循环。所有矩阵有像这样的随机值:

I am a new to MATLAB. I have generated n smaller matrices of numbers, say 3 x 1 by using a FOR loop. All the matrices are having random values like so:

m1 = [3;2;1];   
m2 = [5;1;6];
m3 = [0.2;0.8;7]
m4 = [8;3;0] 
m5 = [3;7;6]
m6 = [8;2;1.3].

现在我想所有的值连接成一个更大的矩阵 M ,使得 M 可重新presented是这样的:

Now I want to concatenate all the values into a larger matrix M such that M can be represented like this:

M = [m1 m2 m3; m4 m5 m6]

M 的输出应为:

M = [3 5 0.2;
     2 1 0.8;
     1 6 7;
     8 3 8; 
     3 7 2;
     0 6 1.3];

我如何初始化使用循环或其他任何东西。每次计数器值即的提高I ,这将导致一个新的矩阵的插入( M1 2 &放大器;等等)里面的更大的矩阵即 M

How do I initialize that by using a FOR loop or anything else so that every time the increase of the counter value i.e i, this will result in an insertion of a new matrix (m1,m2 & so on) inside the bigger matrix i.e M?

注意 M 是一个非常大的基质(​​40左右也许×40),因此我有很多更小的矩阵。

Note that M is a very large matrix (maybe around 40 x 40) and so I am having a lot of smaller matrices.

推荐答案

这似乎非常低效的方式把矩阵一起,但每一个MatLab的新手应该通过这一阶段他的进化。
如果您使用的循环,你应该创建的,因为它们可以使用循环变量进行索引这样的矩阵,否则没有一点使用循环。试试电池阵列,例如:

This seems as highly inefficient way to put matrices together, but every MatLab newbie should pass through this stage in his evolution. If you use for loop, you should create your matrices in such a way that they can be indexed using your loop variable, otherwise there is no point to use the loop. Try cell arrays, for example:

m{1}=[3;2;1];
m{2}=[5;1;6];
m{3}=[.2;.8;7];
m{4}=[8;3;0];
m{5}=[3;7;6];
m{6}=[8;2;1.3];

现在您可以在它们合并为循环:

Now you can merge them in a for loop:

M = [];
NBlocks = length(m) / 3;
for b=1:NBlocks
    M = [M; [m{(b-1)*3+1} m{(b-1)*3+2} m{(b-1)*3+3}] ];
end

注意的这code是非常低效的,特别是对于大型矩阵,只为教育目的提供。考虑重新设计你的任务是使用矩阵preallocation您 M 矩阵。

NOTE This code is highly inefficient, especially for big matrices, and provided only for educational purposes. Consider redesigning your task to use matrix preallocation for your M matrix.

这篇关于形成了从小型矩阵的N个大矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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