1function C = reshape (G, varargin)
2%RESHAPE reshape a matrix.
3% C = reshape (G, m, n) or C = reshape (G, [m n]) returns the m-by-n
4% matrix whose elements are taken columnwise from G.  The matrix G must
5% have numel (G) == m*n.  That is numel (G) == numel (C) must be true.
6%
7% See also GrB/numel, squeeze.
8
9% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
10% SPDX-License-Identifier: GPL-3.0-or-later
11
12% FUTURE: this would be faster as a built-in GxB_reshape function.
13
14if (isobject (G))
15    G = G.opaque ;
16end
17
18[mold, nold, ~] = gbsize (G) ;
19mold = int64 (mold) ;
20nold = int64 (nold) ;
21
22[mnew, nnew] = gb_parse_dimensions (varargin {:}) ;
23mnew = int64 (mnew) ;
24nnew = int64 (nnew) ;
25
26if (mold * nold ~= mnew * nnew)
27    error ('number of elements must not change') ;
28end
29
30desc.base = 'zero-based' ;
31[iold, jold, x] = gbextracttuples (G, desc) ;
32% convert i and j from 2D (mold-by-nold) to 1D indices
33k = gb_convert_index_2d_to_1d (iold, jold, mold) ;
34% convert k from 1D indices to 2D (mnew-by-nnew)
35[inew, jnew] = gb_convert_index_1d_to_2d (k, mnew) ;
36% rebuild the new matrix
37C = GrB (gbbuild (inew, jnew, x, mnew, nnew, desc)) ;
38
39