473,395 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

construct a matrix by some given values

Dear perl Forum,

I have no clue how to start this. I have a file with values like this:
A 2 - 4
B 3 - 5
C 1 - 6

up to the end

I need to formulate a matrix like this

ID 1 2 3 4 5 6 7 8 9 10
A 0 1 1 1 0 0 0 0 0 0
B 0 0 1 0 0 0 0 0 0 0
C 1 1 1 1 1 1 0 0 0 0

up to the end

print one in the range of the value the individual have and print 0 their before and after it. I have no clue how to start doing this.

If you guide me this will be a great help.

Thanks,
Dalila
Jun 8 '07 #1
11 1205
miller
1,089 Expert 1GB
Greetings Dalila,

One way would be the following:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2.  
  3. # Goal:
  4. # ID 1 2 3 4 5 6 7 8 9 10
  5. # A 0 1 1 1 0 0 0 0 0 0
  6. # B 0 0 1 0 0 0 0 0 0 0
  7. # C 1 1 1 1 1 1 0 0 0 0
  8.  
  9. print "ID " . join(' ', (1..10)) . "\n";
  10.  
  11. while (<DATA>) {
  12.     if (! /(\w+) (\d+) - (\d+)/) {
  13.         print "Invalid line format: $_";
  14.  
  15.     } else {
  16.         my $id = $1;
  17.         my $min = $2;
  18.         my $max = $3;
  19.  
  20.         print "$id ";
  21.         print join ' ', map {$_ >= $min && $_ <= $max ? 1 : 0} (1..10);
  22.         print "\n";
  23.     }
  24. }
  25.  
  26. __DATA__
  27. A 2 - 4
  28. B 3 - 5
  29. C 1 - 6
  30.  
Output is:
Expand|Select|Wrap|Line Numbers
  1. >perl scratch.pl
  2. ID 1 2 3 4 5 6 7 8 9 10
  3. A 0 1 1 1 0 0 0 0 0 0
  4. B 0 0 1 1 1 0 0 0 0 0
  5. C 1 1 1 1 1 1 0 0 0 0
  6.  
- Miller
Jun 8 '07 #2
Many thanks Miiler,
I will try it. If the values are irregulary incremented, I think I can put them into array and use the function map as you guided me?
I really very thankful for your guide.
Dalila
Jun 8 '07 #3
miller
1,089 Expert 1GB
Nothing says that you have to use map. That is simply the method that I chose. You could have just as easily used a for loop.

- Miller
Jun 8 '07 #4
KevinADC
4,059 Expert 2GB
There are also a bunch of matrix modules on cpan that might be worth looking into.
Jun 9 '07 #5
Hi Kevin and Miller,
I have done the following and it was working perfectly

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4.  
  5. open(FILE, "IDs.txt");
  6. open(OUT, ">matrix.txt");
  7.  
  8. my @array = (1 , 2 , 3 , 4, 8 , 9);
  9. print OUT "ID", "\t" . join("\t", (@array)) . "\n";
  10.  
  11. while(defined(my $line=<FILE>)) {
  12.     chomp($line);
  13.     my @data = split("\t", $line);
  14.     # to make sure the IDs and intervals are seprated by tabs
  15.     if (!($line=~ /(\w+)(\t)(\d+)(\t)(\d+)/)) {
  16.         print "Invalid line format: $_", "\n";
  17.     } else {
  18.         my $id = $data[0];
  19.         my $min = $data[1];
  20.         my $max = $data[2];
  21.  
  22.         print OUT ($id , "\t");
  23.  
  24.         print OUT join "\t" , map {$_ >= $min && $_ <= $max ? 1 : 0} (@array);
  25.         print "\n";
  26.     }
  27. }
  28.  
I read the IDs and interval from files and reprinted them as you guided me. However, I found some IDs repeated more than once
e.g

A 1 2
A 8 9

I want to print all the intervals for each ID in one file. So, will have
ID 1 2 3 4 8 9
A 1 1 0 0 1 1

Can you help me in a clever way of doing it.
Thanks,
Dalila
Jun 27 '07 #6
KevinADC
4,059 Expert 2GB
is the input data different than your origianl question? Before it was ranges (n-n) , now it looks like individual values (n n) . Do you really want each id (A B C etc) as a seperate output file?
Jun 27 '07 #7
is the input data different than your origianl question? Before it was ranges (n-n) , now it looks like individual values (n n) . Do you really want each id (A B C etc) as a seperate output file?
Hi Miller,
THe input data will be like this (I relaized I have some headers not in interval)

A 1 2
A 7 10
A 15 20
B 3 5
B 11 15
C 5 10
D 10 20

I found some intervalsc not to be equal, it is a fixed set of numbers
e.g @array = (1 , 2, 3 , 5 , 7, 10 , 11, 15 , 20)

I want all the IDs (A, B, C, D) to be in one output file, but each in only one line:
Aim

ID 1 2 3 5 7 10 11 15 20
A 1 1 0 0 1 1 0 1 1
B 0 0 1 1 0 0 1 1 0
C 0 0 0 1 1 1 0 0 0
D 0 0 0 0 0 1 1 1 1

So, all the vlaues will be "map" to the arrays but do not know how to look further to see if the Id is coming again (Id could be repeated with different values for up to 100 times! or more no fixed rule)

I tried to work it out but seems little but hard for me.
Thanks,
Dalila
Aug 18 '07 #8
KevinADC
4,059 Expert 2GB
hmm... I thought you had abondoned this thread. My interest has wanned during the interlude.
Aug 18 '07 #9
Hi Kevin,
I really apologize. If any one can help this will be great. Pleaaaaaaaaaaaaaase help!
Dalila
Aug 18 '07 #10
Thanks Guy,
I have figured it out!!
Aug 19 '07 #11
KevinADC
4,059 Expert 2GB
You figured it out? I think our buddy Travis on Tek-Tips figured it out for you. ;)
Aug 20 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Duncan M Gunn | last post by:
Hi, I need to store the following matrix of values: T U V A 2 1 1 B 2 - - C - - 2 Where (-) is an empty cell.
4
by: L. | last post by:
Hello, I need to generate random Matrices (say of size 5*5), each with an average of X (say X=0.5), but with different values’ range. One matrix should have values in the range of 0-1, while...
16
by: Martin Jørgensen | last post by:
Hi, I've made a program from numerical recipes. Looks like I'm not allowed to distribute the source code from numerical recipes but it shouldn't even be necessary to do that. My problem is...
20
by: Frank-O | last post by:
Hi , Recently I have been commited to the task of "translating" some complex statistical algorithms from Matlab to C++. The goal is to be three times as fast as matlab ( the latest) . I've...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
2
by: DarrenWeber | last post by:
Below is a module (matrix.py) with a class to implement some basic matrix operations on a 2D list. Some things puzzle me about the best way to do this (please don't refer to scipy, numpy and...
8
by: Matt | last post by:
Hello. I'm struggling to work out what bit of matrix manipulation the following piece of code is doing to the 3x3 matrix given: #include <stdio.h> int main() { int check_mat = { { 1 , 2...
4
by: RSH | last post by:
Okay my math skills aren't waht they used to be... With that being said what Im trying to do is create a matrix that given x number of columns, and y number of possible values i want to generate...
2
by: devnew | last post by:
hi i am looking for some info about mapping btw values in an array and corresponding columns of a matrix i have an numpy array= and a numpy matrix object= matrix((, , , ))
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.