File: cell2mat.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 (81 lines) | stat: -rw-r--r-- 2,608 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
function C = cell2mat (A)
%GRB.CELL2MAT Concatenate a cell array of matrices into a single matrix.
% C = GrB.cell2mat (A) converts a 2D cell array of matrices into a single
% @GrB matrix.  The input matrices in A may be @GrB/built-in
% matrices, in any combination.  Let [m,n] = size(A) be the size of the
% cell array A.  Then C is computed as:
%
%  C = [ A{0,0}   A{0,1}   A{0,2}   ... A{0,n-1}
%        A{1,0}   A{1,1}   A{1,2}   ... A{1,n-1}
%        ...
%        A{m-1,0} A{m-1,1} A{m-1,2} ... A{m-1,n-1} ]
%
% If the matrices in A have different types, the type is determined
% according to the rules in GrB.optype.
%
% Note: The methods in the "cat" family include horzcat, vertcat, cat,
% cell2mat (this method), mat2cell, and num2cell.  All of them appear in
% @GrB, and all but this one are overloaded methods.  GrB.cell2mat is a
% static method, since its input is a cell array, not a @GrB object, and
% thus its use cannot trigger the call to an overloaded method.  The
% built-in cell2mat method fails if any A{i,j} entry is a @GrB
% matrix, and it also requires that all entries in A have the same data
% type.  GrB.cell2mat method can operate on any mix of @GrB/built-in
% matrices, with any mix of data types.
%
% Example:
%
%   A = { [1] [2 3 4] ; [5 ; 9] [6 7 8 ; 10 11 12] } ;
%   C1 = cell2mat (A)
%   C2 = GrB.cell2mat (A)
%   C3 = [ A{1,1} A{1,2} ; A{2,1} A{2,2} ]
%   assert (isequal (C1, C2))
%   assert (isequal (C1, C3))
%
%   % mixing data types:
%   A{1,1} = GrB (1, 'single')
%   for k = 1:numel (A)
%       fprintf ('A {%d} is class: %s, type: %s\n', k, ...
%           class (A {k}), GrB.type (A {k})) ;
%   end
%   C2 = GrB.cell2mat (A)
%   assert (isequal (C1, C2))
%   A{1,1} = single (1)
%   A
%   for k = 1:numel (A)
%       fprintf ('A {%d} is class: %s\n', k, class (A {k})) ;
%   end
%   try
%       % the built-in cell2mat does not support mixing of data types.
%       C = cell2mat (A)
%   catch me
%       fprintf ('expected error:\n') ;
%       me
%   end
%
% See also GrB/horzcat, GrB/vertcat, GrB/cat, GrB/mat2cell, GrB/num2cell.

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

if (~iscell (A))
    error ('GrB:error', 'input must be a cell array') ;
end
if (ndims (A) > 2) %#ok<ISMAT>
    error ('GrB:error', 'only 2D cell arrays are supported') ;
end

% get the input matrices
[m, n] = size (A) ;
for j = 1:n
    for i = 1:m
        Tile = A {i,j} ;
        if (isobject (Tile))
            A {i,j} = Tile.opaque ;
        end
    end
end

% concatenate the matrices
C = GrB (gbcat (A)) ;