Mercurial > forge
changeset 8612:10a9d3a169b9 octave-forge
Mechanics. Help and tests for ind2sub_tril and sub2ind_tril
author | jpicarbajal |
---|---|
date | Thu, 20 Oct 2011 12:53:24 +0000 |
parents | 017c701b7bff |
children | 5f1216adf77a |
files | main/mechanics/inst/private/ind2sub_tril.m main/mechanics/inst/private/sub2ind_tril.m |
diffstat | 2 files changed, 86 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/main/mechanics/inst/private/ind2sub_tril.m Thu Oct 20 11:15:27 2011 +0000 +++ b/main/mechanics/inst/private/ind2sub_tril.m Thu Oct 20 12:53:24 2011 +0000 @@ -1,4 +1,4 @@ -%% Copyright (c) 2010 Juan Pablo Carbajal <carbajal@ifi.uzh.ch> +%% Copyright (c) 2011 Juan Pablo Carbajal <carbajal@ifi.uzh.ch> %% %% This program is free software: you can redistribute it and/or modify %% it under the terms of the GNU General Public License as published by @@ -15,13 +15,45 @@ %% -*- texinfo -*- %% @deftypefn {Function File} {[ @var{r}, @var{c} ] = } ind2sub_tril (@var{N}, @var{idx}) -%% Not documented +%% Convert a linear index to subscripts of a trinagular matrix. +%% +%% An example of trinagular matrix linearly indexed follows +%% +%% @example +%% N = 4; +%% A = -repmat (1:N,N,1); +%% A += repmat (diagind, N,1) - A.'; +%% A = tril(A) +%% => A = +%% 1 0 0 0 +%% 2 5 0 0 +%% 3 6 8 0 +%% 4 7 9 10 +%% @end example +%% +%% The following example shows how to convert the linear index `6' in +%% the 4-by-4 matrix of the example into a subscript. +%% +%% @example +%% [r, c] = ind2sub_tril (4, 6) +%% => r = 2 +%% c = 3 +%% @end example +%% +%% when @var{idx} is a row or column matrix of linear indeces then @var{r} and +%% @var{c} have the same shape as @var{idx}. %% %% @seealse{vech, ind2sub, sub2ind_tril} %% @end deftypefn function [r c] = ind2sub_tril(N,idx) + %% Horrible lengthly check + if nargin < 2 ||( !isnumeric (N) && all(size(N)==1) )|| ... + !( ismatrix (idx) && any (size (idx)==1) ) + print_usage; + endif + endofrow = 0.5*(1:N) .* (2*N:-1:N + 1); c = lookup(endofrow, idx-1)+1; @@ -35,8 +67,14 @@ %!test %! A = -repmat (1:N,N,1); -%! A += A.' + repmat (diagind, N,1)); +%! A += repmat (diagind, N,1) - A.'; +%! A = tril(A); %! [r c] = ind2sub_tril (rows(A),1:10); %! A_shouldbe = accumarray([r; c]',1:10); -%! assert (A,A_shouldbe) +%! assert (A_shouldbe, A) +%!error ind2sub_tril([1 2 3],[4 5 6]); % size is not a scalar +%!error ind2sub_tril(4,eye(2)); % idx is not a row or column matrix +%!error ind2sub_tril(4,{1, 2}); % idx is not a row or column matrix +%!error ind2sub_tril([1 2 3]); % not enough arguments +
--- a/main/mechanics/inst/private/sub2ind_tril.m Thu Oct 20 11:15:27 2011 +0000 +++ b/main/mechanics/inst/private/sub2ind_tril.m Thu Oct 20 12:53:24 2011 +0000 @@ -1,4 +1,4 @@ -%% Copyright (c) 2010 Juan Pablo Carbajal <carbajal@ifi.uzh.ch> +%% Copyright (c) 2011 Juan Pablo Carbajal <carbajal@ifi.uzh.ch> %% %% This program is free software: you can redistribute it and/or modify %% it under the terms of the GNU General Public License as published by @@ -15,13 +15,46 @@ %% -*- texinfo -*- %% @deftypefn {Function File} {ind = } sub2ind_tril (@var{N}, @var{r}, @var{c}) -%% Not documented +%% Convert subscripts to a linear index of a trinagular matrix. +%% +%% An example of trinagular matrix linearly indexed follows. %% -%% @seealse{vech, sub2ind} +%% @example +%% N = 4; +%% A = -repmat (1:N,N,1); +%% A += repmat (diagind, N,1) - A.'; +%% A = tril(A) +%% => A = +%% 1 0 0 0 +%% 2 5 0 0 +%% 3 6 8 0 +%% 4 7 9 10 +%% @end example +%% +%% The following example shows how to convert the two-dimensional +%% index `(3,2)' of the 4-by-4 matrix in the example to a linear index. +%% +%% @example +%% linear_index = sub2ind_tril (4, 3, 2) +%% => linear_index = 6 +%% @end example +%% +%% When @var{r} and @var{c} are row or column matrices of subindeces of the same +%% size then @var{ind} have the same shape as any of them. +%% +%% @seealse{vech, sub2ind, ind2sub_tril} %% @end deftypefn function ind = sub2ind_tril(N,r,c) + %% Horrible lengthly check + if nargin < 3 ||( !isnumeric (N) && all(size(N)==1) )|| ... + !( ismatrix (r) && any (size (r)==1) ) || ... + !( ismatrix (c) && any (size (c)==1) ) || ... + any(size(r) != size(c)) + print_usage; + endif + R = zeros(size(r)); C = zeros(size(c)); @@ -49,3 +82,11 @@ %! [r c] = ind2sub_tril (N, 1:N*(N+1)/2); %! assert (sub2ind_tril (N,r',c'), vech (A)) % Full matrix +%!error sub2ind_tril([1 2 3], [4 5 6], [1 2 3]); % size is not a scalar +%!error sub2ind_tril(4, eye(2), [1 2]); % r is not a row or column matrix +%!error sub2ind_tril(4, [1 2], eye(2)); % c is not a row or column matrix +%!error sub2ind_tril(4, [1 2], [1 2 3]); % c,r not the same size +%!error sub2ind_tril(4, [1 2 3 ], 1); % c,r not the same size +%!error sub2ind_tril(4,{1, 2}, {1, 2}); % idx is not a row or column matrix +%!error sub2ind_tril([1 2 3]); % not enough arguments +%!error sub2ind_tril(3,[1 2 3]); % not enough arguments