Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 1tst2
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 1tst3
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 1tst2 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 5from 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 5from 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 5In your opinion, is it possible to do that?
Thanks for any suggestion :)

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();
}

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)...

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.

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...

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 problemThanks 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 1N 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 1N 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 1Where the no neighbor numbers were replaced by N, and
the 6 values were replaced by S.
Thanks to everyone who tried to help me :)

![]() |
![]() |
![]() |

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