concentation in array in matlab

To concatenate arrays in MATLAB, you can use the square brackets [] operator. Here are a couple of different approaches:

  1. Concatenating arrays horizontally: To concatenate arrays horizontally, you can place the arrays next to each other inside the square brackets. The resulting array will have the same number of rows as the original arrays, but the columns will be appended.

    main.m
    % Example arrays
    A = [1, 2, 3];
    B = [4, 5, 6];
    
    % Concatenation
    C = [A, B];
    
    % Display the result
    disp(C);
    
    107 chars
    10 lines

    Output:

    main.m
    1   2   3   4   5   6
    
    22 chars
    2 lines
  2. Concatenating arrays vertically: To concatenate arrays vertically, you can separate the arrays with a semicolon (;) inside the square brackets. The resulting array will have the same number of columns as the original arrays, but the rows will be appended.

    main.m
    % Example arrays
    A = [1, 2, 3];
    B = [4, 5, 6];
    
    % Concatenation
    C = [A; B];
    
    % Display the result
    disp(C);
    
    107 chars
    10 lines

    Output:

    main.m
    1   2   3
    4   5   6
    
    20 chars
    3 lines

Make sure the arrays you want to concatenate have compatible dimensions, otherwise MATLAB will throw an error.

related categories

gistlibby LogSnag