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
11 1205
Greetings Dalila,
One way would be the following: -
use strict;
-
-
# Goal:
-
# 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
-
-
print "ID " . join(' ', (1..10)) . "\n";
-
-
while (<DATA>) {
-
if (! /(\w+) (\d+) - (\d+)/) {
-
print "Invalid line format: $_";
-
-
} else {
-
my $id = $1;
-
my $min = $2;
-
my $max = $3;
-
-
print "$id ";
-
print join ' ', map {$_ >= $min && $_ <= $max ? 1 : 0} (1..10);
-
print "\n";
-
}
-
}
-
-
__DATA__
-
A 2 - 4
-
B 3 - 5
-
C 1 - 6
-
Output is: -
>perl scratch.pl
-
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 1 1 0 0 0 0 0
-
C 1 1 1 1 1 1 0 0 0 0
-
- Miller
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
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
There are also a bunch of matrix modules on cpan that might be worth looking into.
Hi Kevin and Miller,
I have done the following and it was working perfectly -
#!/usr/bin/perl -w
-
-
use strict;
-
-
open(FILE, "IDs.txt");
-
open(OUT, ">matrix.txt");
-
-
my @array = (1 , 2 , 3 , 4, 8 , 9);
-
print OUT "ID", "\t" . join("\t", (@array)) . "\n";
-
-
while(defined(my $line=<FILE>)) {
-
chomp($line);
-
my @data = split("\t", $line);
-
# to make sure the IDs and intervals are seprated by tabs
-
if (!($line=~ /(\w+)(\t)(\d+)(\t)(\d+)/)) {
-
print "Invalid line format: $_", "\n";
-
} else {
-
my $id = $data[0];
-
my $min = $data[1];
-
my $max = $data[2];
-
-
print OUT ($id , "\t");
-
-
print OUT join "\t" , map {$_ >= $min && $_ <= $max ? 1 : 0} (@array);
-
print "\n";
-
}
-
}
-
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
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?
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
hmm... I thought you had abondoned this thread. My interest has wanned during the interlude.
Hi Kevin,
I really apologize. If any one can help this will be great. Pleaaaaaaaaaaaaaase help!
Dalila
Thanks Guy,
I have figured it out!!
You figured it out? I think our buddy Travis on Tek-Tips figured it out for you. ;)
Sign in to post your reply or Sign up for a free account.
Similar topics
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.
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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((,
,
,
))
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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...
| |