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? Among other things, the function offers an
alternative to the duplicate_matrix function when passing from
vech(A.') to A. It is also useful when a matrix of differences of many
points is stored and queried.
That is
px = rand(3,1);
py = rand(3,1);
Rx = vech(bsxfun(@minus,px,px'));
Ry = vech(bsxfun(@minus,py,py'));
D = sqrt(Rx .^2 + Ry.^2);

Which points are at least at distance d=0.2?
d=0.2;
idx = find(D>=d)';
[p1 p2] = sub2ind_triu(size(p,1),idx)

Then p1 and p2 contain the label of the points that follows the criteria.

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

------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2dcopy2
_______________________________________________
Octave-dev mailing list
Octave-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/octave-dev

Reply via email to