1 ## Copyright (C) 2011 Lukas F. Reichlin
3 ## This program is free software: you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation, either version 3 of the License, or
6 ## (at your option) any later version.
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program. If not, see <http://www.gnu.org/licenses/>.
17 ## Subscripted assignment for quaternions.
18 ## Used by Octave for "q.key = value".
20 ## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
21 ## Created: November 2011
24 function q = subsasgn (q, idx, val)
27 case "()" # q(...) = val
28 if (length (idx(1).subs) == 1 && isa (val, "quaternion")) # required by horzcat, vertcat, cat, ...
29 q(idx(1).subs{:}) = val; # q = cellfun (@quaternion, varargin)
31 val = quaternion (val);
32 w = subsasgn (q.w, idx, val.w);
33 x = subsasgn (q.x, idx, val.x);
34 y = subsasgn (q.y, idx, val.y);
35 z = subsasgn (q.z, idx, val.z);
36 q = quaternion (w, x, y, z);
40 if (! is_real_array (val))
41 error ("quaternion: subsasgn: invalid argument type, require real array");
43 if (! size_equal (subsref (q.w, idx(2:end)), val))
44 error ("quaternion: subsasgn: invalid argument size [%s], require dimensions [%s]", \
45 num2str (size (val), "%d "), num2str (size (subsref (q.w, idx(2:end))), "%d "));
47 switch (tolower (idx(1).subs))
49 q.w = subsasgn (q.w, idx(2:end), val);
51 q.x = subsasgn (q.x, idx(2:end), val);
53 q.y = subsasgn (q.y, idx(2:end), val);
55 q.z = subsasgn (q.z, idx(2:end), val);
57 error ("quaternion: subsasgn: invalid subscript name '%s'", idx(1).subs);
61 error ("quaternion: subsasgn: invalid subscript type '%s'", idx(1).type);