Computing.Net > Forums > Unix > A complicated problem :(

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

A complicated problem :(

Reply to Message Icon

Name: abd73fr
Date: January 12, 2005 at 06:10:37 Pacific
OS: Mac OS X.3
CPU/Ram: PowerPc G4/512 MB
Comment:

Hi,

I have three text images: tst1, tst2 and tst3. They are
superposed. tst2 in the middle.

tst1
2 1 3 4 5 1 0 2
1 2 1 1 5 2 2 2
2 2 3 2 1 1 1 3
0 1 0 1 0 0 1 1

tst2
2 1 3 4 5 1 0 2
1 3 6 6 6 6 1 2
2 2 3 6 6 6 6 3
0 1 1 2 2 3 1 1

tst3
2 1 3 2 0 2 0 2
1 2 1 5 5 2 2 2
0 1 2 2 1 2 1 3
1 1 0 1 1 2 1 1

tst2 has an irregular shape represented by the numbers 6
(where 6 is supposed to be the value of each pixel of this
shape).
I want to find the neighborhood of this shape.i.e what are
the values around this shape in 3D dimensions (in ALL
directions) => in the same text image and in the above
and under text images.

For this example I have :
from tst1:
1 3 4 5 1 0
2 1 1 5 2 2 2
2 3 2 1 1 1 3
0 1 0 0 1 1

===> 4 times the number 0; 10 times the number 1; 6
times the number 2;
3 times the number 3; 1 time the number 4; 2 times the
number 5

from tst2:
1 3 4 5 1 0
3 1 2
2 3 3
1 2 2 3 1 1

===> 1 time the number 0; 6 times the number 1; 4
times the number 2;
5 times the number 3; 1 time the number 4; 1 times the
number 5

from tst3:
1 3 2 0 2 0
2 1 5 5 2 2 2
1 2 2 1 2 1 3
0 1 1 2 1 1

===> 3 times the number 0; 9 times the number 1; 10
times the number 2;
2 times the number 3; 0 time the number 4; 2 times the
number 5

In your opinion, is it possible to do that?

Thanks for any suggestion :)



Sponsored Link
Ads by Google

Response Number 1
Name: vgersh99
Date: January 12, 2005 at 06:43:00 Pacific
Reply:

nawk -f sbd.awk tst*

here's sbd.awk:

-----------
function summary( i)
{
for ( i in arr)
printf("\t%d times the number %d\n", arr[i], i);
}

NR == 1 { file=FILENAME }

FNR==1 && NR != 1 {
printf("File [%s]\n", file);
summary();
split("", arr);
file=FILENAME;
}

{
for(i=1; i <= NF; i++)
arr[$i]++;
}

END {
printf("File [%s]\n", file);
summary();
}


0

Response Number 2
Name: abd73fr
Date: January 12, 2005 at 08:08:19 Pacific
Reply:

Thanks vgersh99...
But your code count for ALL the elements in each file. I
want to count ONLY the elements which they are up,
down, left, right, and at the corners (in 3D) of the shape
represented by the numbers (6)...


0

Response Number 3
Name: thepubba
Date: January 12, 2005 at 09:14:56 Pacific
Reply:

An interesting problem. It is probably solvable with awk, since you can manipulate a multi-deminsional array. Can't do that in shell scripting. Pascal or C would probably be best for solving the problem. I'd be interested in seeing a solution using a scripting language, but I don't think it will be easy to write.


0

Response Number 4
Name: vgersh99
Date: January 12, 2005 at 13:56:12 Pacific
Reply:

yeah, one would have to write quite a bit of code manipulating matrix data [based on the awk's associative arrays].

Unfortunately I don't have enough time right now to spare...


0

Response Number 5
Name: abd73fr
Date: January 17, 2005 at 09:16:45 Pacific
Reply:

Finally I found a solution for my complicated problem. :)
I got the answer from other forum (in www.tek-tips.com),
but I'll put the solution in this forum also to be
near the problem

Thanks to (futurelet) who wrote this code.

First, I should put the three files into one, like that:

2 1 3 4 5 1 0 2
1 2 1 1 5 2 2 2
2 2 3 2 1 1 1 3
0 1 0 1 0 0 1 1
2 1 3 4 5 1 0 2
1 3 6 6 6 6 1 2
2 2 3 6 6 6 6 3
0 1 1 2 2 3 1 1
2 1 3 2 0 2 0 2
1 2 1 5 5 2 2 2
0 1 2 2 1 2 1 3
1 1 0 1 1 2 1 1


and here is the code, but I had to use NAWK to get the
good results:


BEGIN { Rows = 4 }

{ MaxCol = NF
  layer = int( (NR-1)/Rows )
  row = (NR-1) % Rows
  for (x=1; x<=NF; x++ )
    matrix[ layer, x, row ] = $x
}

END {
  for (row=0; row<Rows; row++)
    for (col=1; col<=MaxCol; col++)
      if (6==matrix[ 1,col,row ])
        examine( col,row )

  for (layer=0; layer<3; layer++)
  { for (row=0; row<Rows; row++)
    { for (col=1; col<=MaxCol; col++)
      { if ( (layer,col,row) in Neighbors )
        { n = Neighbors[layer,col,row]
          if (6==n)
            n="S"
        }
        else
          n = "N"
        printf "%s ", n
      }
      print ""
    }
    print ""
  }
}

function examine( col, row    ,x,y,z,n )
{
  for (z=0; z<3; z++)
    for (y=row-1; y<=row+1; y++)
    { for (x=col-1; x<=col+1; x++)
      {
        if ( (z,x,y) in matrix )
          Neighbors[z,x,y] = matrix[z,x,y]
      }
    }  
}


*****The results:

$ nawk -f AwkForum3 tst
N 1 3 4 5 1 0 N
N 2 1 1 5 2 2 2
N 2 3 2 1 1 1 3
N N 0 1 0 0 1 1

N 1 3 4 5 1 0 N
N 3 S S S S 1 2
N 2 3 S S S S 3
N N 1 2 2 3 1 1

N 1 3 2 0 2 0 N
N 2 1 5 5 2 2 2
N 1 2 2 1 2 1 3
N N 0 1 1 2 1 1

Where the no neighbor numbers were replaced by N, and
the 6 values were replaced by S.
Thanks to everyone who tried to help me :)


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: A complicated problem :(

complicated problem www.computing.net/answers/unix/complicated-problem/7059.html

A tricky Problem.... www.computing.net/answers/unix/a-tricky-problem/3061.html

I am having a permissions problem www.computing.net/answers/unix/i-am-having-a-permissions-problem/5989.html