File: num2cell.m

package info (click to toggle)
suitesparse-graphblas 7.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 67,112 kB
  • sloc: ansic: 1,072,243; cpp: 8,081; sh: 512; makefile: 503; asm: 369; python: 125; awk: 10
file content (57 lines) | stat: -rw-r--r-- 1,659 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
function C = num2cell (A, dim)
%NUM2CELL Convert matrix into cell array.
%
% C = num2cell (A) converts a @GrB matrix A into a cell array C by
% placing each entry of A in a separate cell, C{i,j} = A(i,j).
% C = num2cell (A, 1) creates a 1-by-n cell array, where A is m-by-n,
% and C{j} is the jth column of A; that is, C{j} = A(:,j).
% C = num2cell (A, 2) creates an m-by-1 cell array where C{i} is the
% ith row of A; that is, C{i} = A (i,:).
% C = num2cell (A, [1 2]) constructs a 1-by-1 cell array C with C{1}=A.
% C = num2cell (A, [2 1]) constructs a 1-by-1 cell array C with C{1}=A.',
% the array transpose of A.
%
% See also GrB/horzcat, GrB/vertcat, GrB/cat, GrB.cell2mat, GrB/mat2cell.

% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
% SPDX-License-Identifier: Apache-2.0

if (nargin == 2 && isequal (dim, [1 2]))

    % whole matrix, not transposed
    C = { A } ;

elseif (nargin == 2 && isequal (dim, [2 1]))

    % whole matrix, transposed
    C = { A.' } ;

else

    % split into scalars, rows, or columns
    if (isobject (A))
        A = A.opaque ;
        [m, n] = gbsize (A) ;
    else
        [m, n] = size (A) ;
    end

    if (nargin == 1)
        % split A into scalars
        C = gbsplit (A, ones (m, 1), ones (n, 1)) ;
    elseif (isequal (dim, 1))
        % split A into columns
        C = gbsplit (A, m, ones (n, 1)) ;
    elseif (isequal (dim, 2))
        % split A into rows
        C = gbsplit (A, ones (m, 1), n) ;
    else
        error ('GrB:error', 'unknown option') ;
    end

    % convert each cell back into GrB matrices
    for k = 1:numel(C)
        C {k} = GrB (C {k}) ;
    end
end