RWalib C Array Library User Guide > Searches and Intersections > alibFind
  

alibFind
Locates nonzero (true) values in an array. Commonly, the UCHAR version of this routine is used on arrays of 0's and 1's resulting from conditional operations applied to arrays element-wise. The PV-WAVE API for this routine is the WHERE Function.
Prototypes
void alibFindb( wvlong n, UCHAR *p, wvlong *q )
void alibFinds( wvlong n, short *p, wvlong *q )
void alibFindi( wvlong n, int *p, wvlong *q )
void alibFindl( wvlong n, wvlong *p, wvlong *q )
void alibFindf( wvlong n, float *p, wvlong *q )
void alibFindd( wvlong n, double *p, wvlong *q )
void alibFindc( wvlong n, COMPLEX *p, wvlong *q )
void alibFindz( wvlong n, DCOMPLEX *p, wvlong *q )
Parameters
n — (Input) The number of nonzero values to locate. The first n nonzero values are located. alibCount can be used to determine the total number of these values.
*p — (Input) The source array.
*q — (Input/Output) The n-element array. On return, array q contains the indices of the first n nonzero values in array p.
Example
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "alib.h"
void main() {
   /* make some example data */
   wvlong nb, nc;
   wvlong *jb, *jc;
   UCHAR    b[ 8]={0,1,0,2,0,3,0,4};
   COMPLEX  c[16]={0,0,0,1,0,0,2,0,0,0,3,4,0,0,0,0};
   printf( "\n print the real and complex arrays, \n" );
   printf( " and count the nonzero elements in each" );
      alibinit( NULL, NULL, NULL, NULL );
      alibPrintArrayb( 1, 1, 1, 8, b, NULL );
      alibPrintArrayc( 1, 1, 1, 8, c, NULL );
      nb = alibCountb( 8, b );
      nc = alibCountc( 8, c );
   printf( "\n make arrays to hold the locations of the nonzero values,\n" );
   printf( " and then find these indices and print them" );
      jb = (wvlong*)malloc(nb*sizeof(wvlong));
      jc = (wvlong*)malloc(nc*sizeof(wvlong));
      alibFindb( nb, b, jb );
      alibFindc( nc, c, jc );
      alibPrintArrayl( 1, 1, 1, nb, jb, "%8lld" );
      alibPrintArrayl( 1, 1, 1, nc, jc, "%8lld" );
}
 
Output:
 
 print the real and complex arrays, 
 and count the nonzero elements in each
 
   0   1   0   2   0   3   0   4
 
(  0.000e+00,  0.000e+00) (  0.000e+00,  1.000e+00) (  0.000e+00,  0.000e+00) 
(  2.000e+00,  0.000e+00) (  0.000e+00,  0.000e+00) (  3.000e+00,  4.000e+00) 
(  0.000e+00,  0.000e+00) (  0.000e+00,  0.000e+00) 
 
 make arrays to hold the locations of the nonzero values,
 and then find these indices and print them
 
       1       3       5       7
 
       1       3       5

Version 2017.0
Copyright © 2017, Rogue Wave Software, Inc. All Rights Reserved.