简体   繁体   中英

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. All I want to do is initialize an array, and push arrays onto it. In Ruby, this would be:

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

Done! Unfortunately I can't find a similarly simple solution in 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. 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 :

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

cells =

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

GIYF. It seems that you are looking for horzcat and vertcat . Check out MATLAB's doc at Creating and concatenating matrices. ; from vertcat page:

C = vertcat(A1,...,AN) vertically concatenates arrays 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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