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: Jordi Gutiérrez Hermoso
Subject: Re: [OctDev] sub2ind for Upper triangular matrices
Date: Mon, 26 Sep 2011 16:06:01 -0500

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.


reply via email to

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