help-octave
[Top][All Lists]

## Re: Simple Matrix Manipulation Extressions

 From: Dirk Laurie Subject: Re: Simple Matrix Manipulation Extressions Date: Mon, 10 Nov 1997 11:18:28 +0200 (SAT)

```john wrote:
>
>       Are there any simple expressions for these octave expressions:
> In each case I introduce loops, multiplications or something a bit
> horrible to achieve something fairly simple.
>
> 5. given a (big) matrix A, and (small) vector V, for each element a
>    of A find the number of V elements smaller than a
>
>         count = zeros(size(A)) ;
>         for r=1:n
>           count = count + (V(r) < A) ;
>         endfor
>
>    Is there a way to avoid the loop? we can sort the V if that helps.
>
>    For A a scalar we might do something like
>
>         count = sum( V < A ) ;
>

do_fortran_indexing=1;
[x,j]=sort(A(:));                 % The method works for two vectors
[x,k]=sort([x; v(:)]);
m=1:length(k); m(k)=1:length(k);
count=A; count(j)=m(1:length(j))-(1:length(j));

I suppose this is `a bit horrible' but it does have complexity
less than length(x)*length(v) and there are no loops.  It will
not work properly if the internal sorting algorithm of octave
allows equal elements to exchange places.

Dirk

```