Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

convert a matrix
Hi all,

I have a such input file :

<N+1> <x0> <x1> <x2> ... <xN>
<y0> <z0,0> <z1,0> <z2,0> ... <zN,0>
<y1> <z0,1> <z1,1> <z2,1> ... <zN,1>
<y2> <z0,2> <z1,2> <z2,2> ... <zN,2>

and I would like to get an output file as following:

<x0> <y0> <z0,0>
<x0> <y1> <z0,1>
<x0> <y2> <z0,2>
: : :
<x1> <y0> <z1,0>
<x1> <y1> <z1,1>
<x1> <y2> <z1,2>
: : :
<x2> <y0> <z2,0>
<x2> <y1> <z2,1>
<x2> <y2> <z2,2>
: : :

Thank you in advance for answering.

Best regards,
Stefanie

Report this thread to moderator Post Follow-up to this message
Old Post
Steffi
08-20-07 11:58 PM


Re: convert a matrix
On Mon, 20 Aug 2007 21:15:58 +0200, Steffi wrote:

> Hi all,
>
> I have a such input file :
>
>   	<N+1> <x0> <x1> <x2> ... <xN>
>   	<y0> <z0,0> <z1,0> <z2,0> ... <zN,0>
>   	<y1> <z0,1> <z1,1> <z2,1> ... <zN,1>
>   	<y2> <z0,2> <z1,2> <z2,2> ... <zN,2>
>
> and I would like to get an output file as following:
>
>   	<x0> <y0> <z0,0>
>   	<x0> <y1> <z0,1>
>   	<x0> <y2> <z0,2>
>   	: : :
>   	<x1> <y0> <z1,0>
>   	<x1> <y1> <z1,1>
>   	<x1> <y2> <z1,2>
>   	: : :
>   	<x2> <y0> <z2,0>
>   	<x2> <y1> <z2,1>
>   	<x2> <y2> <z2,2>
>   	: : :
>
> Thank you in advance for answering.
>
> Best regards,
> Stefanie

Hi Steffi, hello netlanders,

a straightforward script, which solves your problem:

NR == 1 {
N = $1 -1
for (i = 2; i <= N; ++i)
x[i-2] = $i
}
NR >= 2 {
y[NR-2] = $1
for (i = 2; i <= N; ++i)
z[i-2, NR-2] = $i
}
END {
for (sect = 0; sect <= NR - 2; ++sect) {
for (i = 0; i <= NR - 2; ++i)
printf "%s\t%s\t%s\n", x[sect], y[i], z[sect,i]
print ":::"
}
}

Enjoy it,

Steffen Schuler

Report this thread to moderator Post Follow-up to this message
Old Post
Steffen Schuler
08-20-07 11:58 PM


Re: convert a matrix
On Mon, 20 Aug 2007 21:11:18 +0000, Steffen Schuler wrote:

> On Mon, 20 Aug 2007 21:15:58 +0200, Steffi wrote:
> 
>
> Hi Steffi, hello netlanders,
>
> a straightforward script, which solves your problem:
>
> NR == 1 {
>   N = $1 -1
>   for (i = 2; i <= N; ++i)
>     x[i-2] = $i
> }
> NR >= 2 {
>   y[NR-2] = $1
>   for (i = 2; i <= N; ++i)
>     z[i-2, NR-2] = $i
> }
> END {
>   for (sect = 0; sect <= NR - 2; ++sect) {
>     for (i = 0; i <= NR - 2; ++i)
>       printf "%s\t%s\t%s\n", x[sect], y[i], z[sect,i]
>     print ":::"
>   }
> }

Corrected script:

NR == 1 {
N = $1 -1
for (i = 2; i <= NF; ++i)
x[i-2] = $i
}
NR >= 2 {
y[NR-2] = $1
for (i = 2; i <= NF; ++i)
z[i-2, NR-2] = $i
}
END {
for (sect = 0; sect <= N; ++sect) {
for (i = 0; i <= NR - 2; ++i)
printf "%s\t%s\t%s\n", x[sect], y[i], z[sect,i]
print ":::"
}
}

Sorry,

Steffen Schuler

Report this thread to moderator Post Follow-up to this message
Old Post
Steffen Schuler
08-20-07 11:58 PM


Re: convert a matrix
Steffi wrote:
> Hi all,
>
> I have a such input file :
>
>      <N+1> <x0> <x1> <x2> ... <xN>
>      <y0> <z0,0> <z1,0> <z2,0> ... <zN,0>
>      <y1> <z0,1> <z1,1> <z2,1> ... <zN,1>
>      <y2> <z0,2> <z1,2> <z2,2> ... <zN,2>
>
> and I would like to get an output file as following:
>
>      <x0> <y0> <z0,0>
>      <x0> <y1> <z0,1>
>      <x0> <y2> <z0,2>
>      : : :
>      <x1> <y0> <z1,0>
>      <x1> <y1> <z1,1>
>      <x1> <y2> <z1,2>
>      : : :
>      <x2> <y0> <z2,0>
>      <x2> <y1> <z2,1>
>      <x2> <y2> <z2,2>
>      : : :
>
> Thank you in advance for answering.
>
> Best regards,
> Stefanie

NR==1 { for (i=2;i<=NF;i++) X[i]=$i; next }
{ Y[NR]=$1; for (i=2;i<=NF;i++) Z[i,NR]=$i }
END { for (x=2;x<=NF;x++)
for (y=2;y<=NR;y++)
print X[x],Y[y],Z[x,y]
}

Regards,

Ed.

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
08-21-07 08:58 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 03:43 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.