Mercurial > hg > octave-nkf
annotate scripts/miscellaneous/substruct.m @ 10509:ddbd812d09aa
properly compress sparse matrices after assembly
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Mon, 12 Apr 2010 12:57:44 +0200 |
parents | b122dd3075ce |
children | 95c3e38098bf |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 2006, 2007 John W. Eaton |
10280 | 2 ## Copyright (C) 2010 VZLU Prague |
6167 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
6167 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
6167 | 19 |
20 ## -*- texinfo -*- | |
6744 | 21 ## @deftypefn {Function File} {} substruct (@var{type}, @var{subs}, @dots{}) |
6167 | 22 ## Create a subscript structure for use with @code{subsref} or |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
23 ## @code{subsasgn}. For example: |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
24 ## |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
25 ## @example |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
26 ## @group |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
27 ## idx = substruct("()", @{3, ":"@}) |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
28 ## @result{} |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
29 ## idx = |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
30 ## @{ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
31 ## type = () |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
32 ## subs = |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
33 ## @{ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
34 ## [1,1] = 3 |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
35 ## [1,2] = : |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
36 ## @} |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
37 ## @} |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
38 ## x = [1, 2, 3; 4, 5, 6; 7, 8, 9]; |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
39 ## subsref(x, idx) |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
40 ## @result{} ans = |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
41 ## 7 8 9 |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
42 ## @end group |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9774
diff
changeset
|
43 ## @end example |
6167 | 44 ## @seealso{subsref, subsasgn} |
45 ## @end deftypefn | |
46 | |
47 ## Author: jwe | |
48 | |
49 function retval = substruct (varargin) | |
50 | |
51 nargs = nargin; | |
52 | |
53 if (nargs > 1 && mod (nargs, 2) == 0) | |
10280 | 54 typ = varargin(1:2:nargs); |
55 sub = varargin(2:2:nargs); | |
56 braces = strcmp (typ, "()") | strcmp (typ, "{}"); | |
57 dots = strcmp (typ, "."); | |
58 if (all (braces | dots)) | |
59 cells = cellfun ("isclass", sub, "cell"); | |
60 chars = cellfun ("isclass", sub, "char"); | |
61 if (any (braces &! cells)) | |
62 error ("substruct: for type == () or {}, subs must be a cell array"); | |
63 elseif (any (dots &! chars)) | |
64 error ("substruct: for type == ., subs must be a character string"); | |
6167 | 65 endif |
10280 | 66 else |
67 error ("substruct: expecting type to be one of \"()\", \"{}\", or \".\""); | |
68 endif | |
69 | |
6167 | 70 retval = struct ("type", typ, "subs", sub); |
71 else | |
72 print_usage (); | |
73 endif | |
74 | |
75 endfunction | |
76 | |
77 %!test | |
78 %! x(1,1).type = "()"; | |
9774
fbf15a0f30f0
Call user-defined subsref/subsasgn with 1xN structs instead of Nx1
David Grundberg <davidg@cs.umu.se>
parents:
7017
diff
changeset
|
79 %! x(1,2).type = "{}"; |
fbf15a0f30f0
Call user-defined subsref/subsasgn with 1xN structs instead of Nx1
David Grundberg <davidg@cs.umu.se>
parents:
7017
diff
changeset
|
80 %! x(1,3).type = "."; |
6167 | 81 %! x(1,1).subs = {1,2,3}; |
10280 | 82 %! x(1,2).subs = {":"}; |
9774
fbf15a0f30f0
Call user-defined subsref/subsasgn with 1xN structs instead of Nx1
David Grundberg <davidg@cs.umu.se>
parents:
7017
diff
changeset
|
83 %! x(1,3).subs = "foo"; |
10280 | 84 %! y = substruct ("()", {1,2,3}, "{}", {":"}, ".", "foo"); |
6167 | 85 %! assert(x,y); |
86 %!error assert(substruct); | |
87 %!error assert(substruct (1, 2, 3)); | |
88 %!error assert(substruct ("x", 1)); | |
89 %!error assert(substruct ("()", [1,2,3])); | |
90 %!error assert(substruct (".", {1,2,3})); |