Finding indices in an array nearest to a set of values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding indices in an array nearest to a set of values
# 1  
Old 07-12-2011
Finding indices in an array nearest to a set of values

I have an two arrays. One array BINDIST consists of fences. I have another array XOFFS.

Eg
Code:
BINDIST = 0 10 20 30 40 50 60

Code:
XOFFS = 2 3 4 23 25 28 55 58

I want to find to find the indices of values in XOFFS that are closest to each BINDIST.

My idea is to do as follows

I create array XMIDS such that

Code:
XMIDS(I) = ( BINDIST(I) + BINDIST(I+1) ) / 2, for I = 1 to NBINDIST-1

Then I check values in XOFFS such that

I find all XOFFS between BINDIST(1) and XMID(1), and choose among these, the closest to BINDIST(1)
Save in VALS(1)

I find all XOFFS between XMIDS(I) and XMIDS(I+1) , and choose among these, the closest to BINDIST(I+1), for I = 1 to NBINDIST-2
Save in VALS(I+1)

I find all XOFFS between XMIDS(NBINDIST-1) and BINDIST(NBINDIST) , and choose among these, the closest to BINDIST(NBINDIST)
Save in VALS(NBINDIST)

If I don't find any XOFFS if the regions described, I return ZERO to the corresponding VALS.

Result should be

Code:
VALS = 2 0 23 28 0 55 58


Last edited by kristinu; 07-12-2011 at 07:51 PM..
# 2  
Old 07-12-2011
what is the results surpposed to be?
# 3  
Old 07-12-2011
Code:
VALS = 2 0 23 28 0 55 58

# 4  
Old 07-12-2011
try:
Code:
echo -e "${XOFFS[@]}\n${BINDIST[@]}" |\
awk  'NR==1{len=split($0,X);next}
{for(i=1;i<=NF;i++){
   t=x=0
   for(j=1;j<=len;j++){
      x=sqrt((X[j]-$i)*(X[j]-$i))
      if(j==1){t=x}else{t=t<x?t:x}
      a[x]=X[j]}
      printf  t<=5?a[t]" ":"0 "
      delete a}
   printf "\n"}'
2 0 23 28 0 55 58

# 5  
Old 07-12-2011
Coded the following csh script and got the correct result

Code:
2 0 23 28 0 55 58

Code:
#!/bin/csh

set XOFFS = (2 3 4 23 25 28 55 58)
set BINDIST = (0 10 20 30 40 50 60)
echo $XOFFS
echo "${XOFFS}\n${BINDIST}" |        \
  awk  'NR == 1 { len = split($0, X); next }  \
    { for (i=1; i<=NF; i++) {           \
      t = x = 0                         \
      for (j=1; j<=len; j++) {          \
        x = sqrt((X[j]-$i)*(X[j]-$i))   \
        if(j==1) {t=x} else {t=t<x?t:x} \
        a[x] = X[j]                     \
      }                                 \
      printf t<=5?a[t]" ":"0 "          \
      delete a                          \
    }                                   \
      printf "\n"                       \
    }'

---------- Post updated at 08:35 PM ---------- Previous update was at 08:21 PM ----------

Got no idea how this code is working. Why do you use a[x] and what's the sqrt about. You did not calculate XMIDS

Last edited by kristinu; 07-12-2011 at 10:47 PM..
# 6  
Old 07-12-2011
I don't know if this codes can be run in csh, I did it in bash.

Code:
#!/bin/bash
XOFFS=(2 3 4 23 25 28 55 58) # array 
BINDIST=(0 10 20 30 40 50 60) # array 
echo ${XOFFS[@]} echo -e "${XOFFS[@]}\n${BINDIST[@]}"|\
awk  'NR==1{len=split($0,X);next}
{for(i=1;i<=NF;i++){
   t=x=0
   for(j=1;j<=len;j++){
      x=sqrt((X[j]-$i)*(X[j]-$i))
      if(j==1){t=x}else{t=t<x?t:x}
      a[x]=X[j]}
      printf  t<=5?a[t]" ":"0 "
      delete a}
      printf "\n"}'

# 7  
Old 07-12-2011
It works but trying to figure out the calculations. Values in both XOFFS and BINDIST will have both negative and positive values.
The values can be floating point, not just integers.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get value from array and set those values as a variable

I am new to ksh scripting, specially array. How do i get values from an array and set the value as variable and pass those variables to the different functions?? someone taught me how to get input from a file with have columns i need to read, but now i doesnt know how to set those value to be a... (7 Replies)
Discussion started by: gavin_L
7 Replies

2. Shell Programming and Scripting

Finding compound words from a set of files from another set of files

Hi All, I am completely stuck here. I have a set of files (with names A.txt, B.txt until L.txt) which contain words like these: computer random access memory computer networking mouse terminal windows All the files from A.txt to L.txt have the same format i.e. complete words in... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

3. UNIX for Dummies Questions & Answers

finding nearest value in a column

Hi, I have 2 files: file1: 1 ia 2 1 mn 6 1 sd 11 2 ny 3 2 ma 10 3 wa 7 3 ca 8 file2 1 mi 3 1 wi 5 2 pa 4 3 id 6 (2 Replies)
Discussion started by: peanuts48
2 Replies

4. Programming

Finding range of values in an array

I have an array containing distances in ascending order, for example: distances = 100 120 150 170 200 280 300 .... I have a number, let's say v = 170 and a variation value, let's say var = 100 . I want to return the array indexes for which the distances cover the range (v - var) to (v +... (3 Replies)
Discussion started by: kristinu
3 Replies

5. SCO

Help finding where certain environment variables are set

i have two machines that should be identical but on one system there are some oracle environment (ORACLE_SID, ORACLE_HOME, etc...) variables that are not being set for the users. I am trying to find where those environment variables are being set on the system which is working properly. All... (5 Replies)
Discussion started by: kuliksco
5 Replies

6. Shell Programming and Scripting

Finding the most frequently occurring set of words

Hi guys, I have a file with a list of phoneme for words, it looks like this: AILS EY1 L Z AIMLESSLY EY1 M L AH0 S L IY0 AIMONE EY1 M OW2 N AIMS EY1 M Z AINGE EY1 NG AINGE(2) EY1 N JH AINLEY EY1 N L IY0 AINSLIE EY1 N Z L IY0 AIR EH1 R AIRBAGS EH1 R B AE2 G Z and I need to... (5 Replies)
Discussion started by: Andrew9191
5 Replies

7. Shell Programming and Scripting

PHP: Search Multi-Dimensional(nested) array and export values of currenly worked on array.

Hi All, I'm writing a nagios check that will see if our ldap servers are in sync... I got the status data into a nested array, I would like to search key of each array and if "OK" is NOT present, echo other key=>values in the current array to a variable so...eg...let take the single array... (1 Reply)
Discussion started by: zeekblack
1 Replies

8. UNIX for Advanced & Expert Users

Finding register set being used in program

How can i find( or list) contents of all registers being used by my program? Is there any system call or library available for this?:confused: At runtime in my c/c++ program. At runtime using may be some assembly hack!!!!!!!!!!! (2 Replies)
Discussion started by: amit gangarade
2 Replies

9. Shell Programming and Scripting

Finding Max value from an array

Hi, I need to find max and second max element from an array. array contains 0338,0337,0339,0340,0401,0402,0403 (10 Replies)
Discussion started by: vjasai
10 Replies

10. Shell Programming and Scripting

Max amount of awk array indices

Does anyone know what the max amount of indices you can store in a awk array? (0 Replies)
Discussion started by: timj123
0 Replies
Login or Register to Ask a Question