octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [OctDev] sub2ind for Upper triangular matrices


From: JuanPi
Subject: Re: [OctDev] sub2ind for Upper triangular matrices
Date: Wed, 12 Oct 2011 12:05:11 +0200

2011/9/26 Jordi Gutiérrez Hermoso <address@hidden>:
> On 24 September 2011 17:33, JuanPi <address@hidden> wrote:
>> Hi
>>
>> >From a discussion in the IRC channel concerning this post
>> http://stackoverflow.com/questions/242711/algorithm-for-index-numbers-of-triangular-matrix-coefficients/3148414#3148414
>>
>> JordiGH improved the algorithm and include it in sprandsym, though he
>> mentioned that there may be problem with rounding real numbers.
>> Therefore user deeego proposed the use of lookup. JorgdiGH mentioned
>> that the two version had similar profiling times and that deego's
>> suggestion was more robuts cause it doen't suffer form rounding
>> problems. I wrote the function and I post it here
>>
>> function [r c] = ind2sub_triu (N, idx)
>>  endofrow = (1:N) .* (N - (0:(N - 1)) / 2);
>>  r = lookup(endofrow, idx-1)+1;
>>
>>  c = N - endofrow(r) + idx;
>> end
>>
>> %!test
>> %! A = [1 2 3 4; 0 5 6 7; 0 0 8 9; 0 0 0 10];
>> %! [r c] = ind2sub_triu (rows(A),1:10);
>> %! A_shouldbe = accumarray([r; c]',1:10);
>> %! assert (A,A_shouldbe)
>>
>> %! assert(A,)
>>
>> Why to add this function?
>
> I kind of like the idea of adding this as a "triu" or "tril" option to
> sub2ind (and to ind2sub). I'm CC'ing the maintainers list to see if
> this could be done or if it's a dangerous idea.
>
> - Jordi G. H.
>

Hi,

Was this behavior of sub2ind pushed?

>From ML documentation,
http://www.mathworks.com/help/techdoc/ref/sub2ind.html

all arguments to sub2ind are supposed to be scalars or arrays.
Therefore a definition of sub2ind
## @deftypefnx {Function File} { @var{ind} = } sub2ind (@dots{}, @var{type})

and a check for string varargin elements

type_str = varargin{ cellfun (@ischar, varargin) }
if !isempty (type_str)

 <Do the new stuff>
 <Transpose according to type_str == 'triu' or type_str == 'tril'>

 return

end

Maintains a behavior compatible with ML.

-- 
JuanPi Carbajal
-----
"Complex problems have simple, easy-to-understand wrong answers."
Murphy’s Law Book Two
-----
http://ailab.ifi.uzh.ch/carbajal/


reply via email to

[Prev in Thread] Current Thread [Next in Thread]