简体   繁体   English

在MATLAB中将数组推到2D数组(矩阵)上

[英]Push array onto 2D array (matrix) in MATLAB

For God only knows what reason, we're being asked to use MATLAB in an AI course. 因为上帝只知道是什么原因,我们被要求在AI课程中使用MATLAB。 All I want to do is initialize an array, and push arrays onto it. 我想做的就是初始化一个数组,然后将数组推到它上面。 In Ruby, this would be: 在Ruby中,这将是:

multi_arr = []
an_arr = [1, 2, 3, 4]
multi_arr << an_arr

Done! 完成! Unfortunately I can't find a similarly simple solution in MATLAB. 不幸的是我在MATLAB中找不到类似的简单解决方案。

Any advice would be extremely appreciated. 任何建议都将非常感激。

EDIT: for the interested, here's the rather ungraceful solution I arrived at: 编辑:对于感兴趣的,这是我到达的相当不合理的解决方案:

child_states = []
child_state = [0,1,2,3,4,5,6,7,8]

% returns [rows, columns]
dimensions = size(child_states)
child_states(dimensions(1)+1, 1:9) = child_state

You can append array to an array in matlab without knowing the dimensions but it won't be very efficient because matlab will allocate space for the whole array each time you do it. 您可以在不知道尺寸的情况下将数组附加到matlab中的数组,但效率不高,因为每次执行时,matlab都会为整个数组分配空间。 Here's how to do it: 这是怎么做的:

arrays = [];
arr1 = [1,2];
arr2 = [3,4,5];
% append first array
arrays  = [arrays ,arr1 ]
% append second array
arrays  = [arrays ,arr2 ]

arrays = 数组=

 1     2

arrays = 数组=

 1     2     3     4     5

if each of the arrays you want to append have the same length, then you can append them as rows: 如果要追加的每个数组具有相同的长度,则可以将它们作为行附加:

arrays = [];
arr1 = [1,2,4];
arr2 = [5,6,7];
% append first array
arrays  = [arrays ; arr1 ]
% append second array
arrays  = [arrays ; arr2 ]

arrays = 数组=

 1     2     4

arrays = 数组=

 1     2     4
 5     6     7

for more of a ruby like array appending you should use cell arrays : 对于更多类似ruby的数组,你应该使用单元格数组

cells = {};
cells = [cells ,[4,5] ]
cells = [cells ,[1,1,1] ]
cells = [cells ,['hello']]

cells = cells =

[1x2 double]    [1x3 double]    'hello'

GIYF. GIYF。 It seems that you are looking for horzcat and vertcat . 看来你正在寻找horzcatvertcat Check out MATLAB's doc at Creating and concatenating matrices. 查看MATLAB的创建和连接矩阵的文档 ; ; from vertcat page: 来自vertcat页面:

C = vertcat(A1,...,AN) vertically concatenates arrays A1,...,AN . C = vertcat(A1,...,AN)垂直连接数组A1,...,AN All arrays in the argument list must have the same number of columns. 参数列表中的所有数组必须具有相同的列数。

If the inputs are multidimensional arrays, vertcat concatenates N-dimensional arrays along the first dimension. 如果输入是多维数组,则vertcat沿第一维连接N维数组。 The remaining dimensions must match. 其余尺寸必须匹配。

Here's a function that's supposed to do what you want: concatenate a row vector to an array regardless of size. 这是一个应该做你想做的功能:将行向量连接到数组而不管大小。 This function will check the dimension along the second axis of input and output array and pad zero to whichever one that is smaller so they can be concatenated along the first axis. 此函数将检查沿输入和输出数组的第二轴的尺寸,并将零填充到任何较小的尺寸,以便它们可以沿第一轴连接。

function m = freevertcat(m, n)

    if isempty(m)
        m = cat(1, m, n);
    else
        size_m = size(m, 2);
        size_n = size(n, 2);
        if size_m > size_n 
            n(size_n+1 : size_n + size_m - size_n) = 0
        elseif size_n > size_m
            m(:, size_m+1 : size_m + size_n - size_m) = 0; 
        end
        m = cat(1, m, n);
end

example usage 示例用法

m = []
n = [1,2,3,4,5]
m = freevertcat(m,n)
p = [3,3,3]
m = freevertcat(m,p)

You'll get 你会得到

m =  1 2 3 4 5
     3 3 3 0 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM