[37] | 1 | function r = subsasgn(a, ss, b) |
---|
| 2 | % SUBSASGN Subscripted assignment. |
---|
| 3 | % (Quaternion overloading of standard Matlab function.) |
---|
| 4 | |
---|
| 5 | % Copyright © 2005 Stephen J. Sangwine and Nicolas Le Bihan. |
---|
| 6 | % See the file : Copyright.m for further details. |
---|
| 7 | |
---|
| 8 | switch ss.type |
---|
| 9 | case '()' |
---|
| 10 | if length(ss) ~= 1 |
---|
| 11 | error('Multiple levels of subscripting are not supported for quaternions.') |
---|
| 12 | end |
---|
| 13 | |
---|
| 14 | if ~isa(a, 'quaternion') |
---|
| 15 | error('Left side must be a quaternion in subscripted assignment if right side is a quaternion.') |
---|
| 16 | end |
---|
| 17 | |
---|
| 18 | if ~isa(b, 'quaternion') |
---|
| 19 | |
---|
| 20 | % Argument a (the left-hand side of the assignment) is a quaternion, |
---|
| 21 | % but b (the right-hand side) is not (it could be double, for example, |
---|
| 22 | % zero). To handle this case, we need to convert b to a quaternion. |
---|
| 23 | |
---|
| 24 | if ispure(a) |
---|
| 25 | error('Cannot convert right-hand argument to a pure quaternion.'); |
---|
| 26 | else |
---|
| 27 | if isnumeric(b) |
---|
| 28 | r = subsasgn(a, ss, quaternion(b)); % Convert b to a quaternion (with implicit |
---|
| 29 | return % zero vector part), and call recursively. |
---|
| 30 | else |
---|
| 31 | error('Cannot convert non-numeric right-hand argument to a quaternion.'); |
---|
| 32 | end |
---|
| 33 | end |
---|
| 34 | end |
---|
| 35 | |
---|
| 36 | if ispure(a) ~= ispure(b) |
---|
| 37 | error('Left and right sides in subscripted assignment must both be pure, or both full quaternions.') |
---|
| 38 | end |
---|
| 39 | |
---|
| 40 | % To implement indexed assignment, we separate the quaternion into components, |
---|
| 41 | % perform the assignments on the components, and then construct the quaternion |
---|
| 42 | % result from the components. |
---|
| 43 | |
---|
| 44 | if ispure(a) |
---|
| 45 | xa = x(a); ya = y(a); za = z(a); |
---|
| 46 | xb = x(b); yb = y(b); zb = z(b); |
---|
| 47 | |
---|
| 48 | xa(ss.subs{:}) = xb; |
---|
| 49 | ya(ss.subs{:}) = yb; |
---|
| 50 | za(ss.subs{:}) = zb; |
---|
| 51 | |
---|
| 52 | r = quaternion(xa, ya, za); |
---|
| 53 | else |
---|
| 54 | sa = s(a); xa = x(a); ya = y(a); za = z(a); |
---|
| 55 | sb = s(b); xb = x(b); yb = y(b); zb = z(b); |
---|
| 56 | |
---|
| 57 | sa(ss.subs{:}) = sb; |
---|
| 58 | xa(ss.subs{:}) = xb; |
---|
| 59 | ya(ss.subs{:}) = yb; |
---|
| 60 | za(ss.subs{:}) = zb; |
---|
| 61 | |
---|
| 62 | r = quaternion(sa, xa, ya, za); |
---|
| 63 | end |
---|
| 64 | case '{}' |
---|
| 65 | error('Cell array indexing is not valid for quaternions.') |
---|
| 66 | case '.' |
---|
| 67 | error('Structure indexing is not implemented for quaternions.') |
---|
| 68 | % |
---|
| 69 | % Possible use of structure indexing is to implement the following |
---|
| 70 | % sorts of assignment: |
---|
| 71 | % |
---|
| 72 | % q.x = blah |
---|
| 73 | % |
---|
| 74 | % However, there are some issues to be considered before implementing |
---|
| 75 | % such a scheme. Would it work, for example, if q doesn't exist? What |
---|
| 76 | % should happen if q is pure and q.s = blah occurs? Should q become a |
---|
| 77 | % full quaternion, or should an error be raised? What about mixed use |
---|
| 78 | % of array indexing and structure indexing, e.g. q.x(:,1)? Would it |
---|
| 79 | % work for q.x = q.x .* 2 where the structure indexing occurs on the |
---|
| 80 | % right hand side as well as on the left. Guess: q.x on the right would |
---|
| 81 | % be handled by subsref, not subsassgn. |
---|
| 82 | % |
---|
| 83 | % (Notes added after discussion with Sebastian Miron, 12 July 2005.) |
---|
| 84 | % |
---|
| 85 | otherwise |
---|
| 86 | error('subsassign received an invalid subscripting type.') |
---|
| 87 | end |
---|