combine cell and double in matlab

To combine a cell array and a numeric array into a single cell array, use the num2cell function on the numeric array and concatenate it with the original cell array using the {} concatenation operator. Here's an example:

main.m
A = {'hello', 'world'};
B = [1, 2, 3];
C = [A, num2cell(B)]
60 chars
4 lines

Output:

main.m
C =

  1×5 cell array

    'hello'    'world'    [1]    [2]    [3]
67 chars
6 lines

In this example, the num2cell function converts the array B into a cell array of the same size, which is then concatenated with A using the {} operator. The resulting array C is a 1x5 cell array containing the elements of A followed by the elements of B as individual cells.

Note that the cell and the numeric arrays must have the same number of elements in this case. If they don't, you may need to adjust the size or shape of one or both arrays to ensure they can be combined without errors.

related categories

gistlibby LogSnag