1function C = num2cell (A, dim)
2%NUM2CELL Convert matrix into cell array.
3%
4% C = num2cell (A) converts a @GrB matrix A into a cell array C by
5% placing each entry of A in a separate cell, C{i,j} = A(i,j).
6% C = num2cell (A, 1) creates a 1-by-n cell array, where A is m-by-n,
7% and C{j} is the jth column of A; that is, C{j} = A(:,j).
8% C = num2cell (A, 2) creates an m-by-1 cell array where C{i} is the
9% ith row of A; that is, C{i} = A (i,:).
10% C = num2cell (A, [1 2]) constructs a 1-by-1 cell array C with C{1}=A.
11% C = num2cell (A, [2 1]) constructs a 1-by-1 cell array C with C{1}=A.',
12% the array transpose of A.
13%
14% See also GrB/horzcat, GrB/vertcat, GrB/cat, GrB.cell2mat, GrB/mat2cell.
15
16% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
17% SPDX-License-Identifier: GPL-3.0-or-later
18
19if (nargin == 2 && isequal (dim, [1 2]))
20
21    % whole matrix, not transposed
22    C = { A } ;
23
24elseif (nargin == 2 && isequal (dim, [2 1]))
25
26    % whole matrix, transposed
27    C = { A.' } ;
28
29else
30
31    % split into scalars, rows, or columns
32    if (isobject (A))
33        A = A.opaque ;
34        [m, n] = gbsize (A) ;
35    else
36        [m, n] = size (A) ;
37    end
38
39    if (nargin == 1)
40        % split A into scalars
41        C = gbsplit (A, ones (m, 1), ones (n, 1)) ;
42    elseif (isequal (dim, 1))
43        % split A into columns
44        C = gbsplit (A, m, ones (n, 1)) ;
45    elseif (isequal (dim, 2))
46        % split A into rows
47        C = gbsplit (A, ones (m, 1), n) ;
48    else
49        error ('unknown option') ;
50    end
51
52    % convert each cell back into GrB matrices
53    for k = 1:numel(C)
54        C {k} = GrB (C {k}) ;
55    end
56end
57
58