Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

consturct a matrix from different intervals (unequal) 1

Status
Not open for further replies.

Mozart07

Programmer
Joined
Aug 19, 2007
Messages
4
Location
GB
Hello all the forum users,
I want to construct a matrix for different IDs having "1" values at variables lying in the interval of the given data and "0" otherwise.
Example:
THe input data will be like this
A 1 2
A 7 10
A 15 20
B 3 5
B 11 15
C 5 10
D 10 20

The headers will be different values of no relation to each other, I have input them in the @array
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, hence put 1 in the corrsponding variables instead of 0? (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 too hard for me!!
Thanks,
 
Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@data[/blue] = [red]([/red][red]"[/red][purple]A 1 2[/purple][red]"[/red], [red]"[/red][purple]A 7 10[/purple][red]"[/red], [red]"[/red][purple]A 15 20[/purple][red]"[/red], [red]"[/red][purple]B 3 5[/purple][red]"[/red], [red]"[/red][purple]B 11 15[/purple][red]"[/red], [red]"[/red][purple]C 5 10[/purple][red]"[/red], [red]"[/red][purple]D 10 20[/purple][red]"[/red][red])[/red][red];[/red]
[black][b]my[/b][/black] [blue]@array[/blue][red];[/red]
[black][b]my[/b][/black] [blue]%out[/blue][red];[/red]
[olive][b]for[/b][/olive] [black][b]my[/b][/black] [blue]$line[/blue] [red]([/red][blue]@data[/blue][red])[/red] [red]{[/red]
	[black][b]my[/b][/black] [blue]@tmp[/blue] = [url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url] [red]/[/red][purple][purple][b]\s[/b][/purple]+[/purple][red]/[/red], [blue]$line[/blue][red];[/red]
	[black][b]my[/b][/black] [blue]$id[/blue] = [url=http://perldoc.perl.org/functions/shift.html][black][b]shift[/b][/black][/url] [blue]@tmp[/blue][red];[/red]
	[olive][b]for[/b][/olive] [black][b]my[/b][/black] [blue]$tmp[/blue] [red]([/red][blue]@tmp[/blue][red])[/red] [red]{[/red]
		[blue]$out[/blue][red]{[/red][blue]$id[/blue][red]}[/red][red]{[/red][blue]$tmp[/blue][red]}[/red] = [fuchsia]1[/fuchsia][red];[/red]
		[olive][b]if[/b][/olive] [red]([/red]! [url=http://perldoc.perl.org/functions/grep.html][black][b]grep[/b][/black][/url] [red]/[/red][purple][blue]$tmp[/blue][/purple][red]/[/red], [blue]@array[/blue][red])[/red] [red]{[/red] 
			[url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@array[/blue], [blue]$tmp[/blue][red];[/red]
		[red]}[/red]
	[red]}[/red]
[red]}[/red]

[blue]@array[/blue] = [url=http://perldoc.perl.org/functions/sort.html][black][b]sort[/b][/black][/url] [red]{[/red][blue]$a[/blue] <=> [blue]$b[/blue][red]}[/red]  [blue]@array[/blue][red];[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]ID [/purple][red]"[/red][red];[/red]
[olive][b]for[/b][/olive] [black][b]my[/b][/black] [blue]$data[/blue] [red]([/red][blue]@array[/blue][red])[/red] [red]{[/red]
	[black][b]my[/b][/black] [blue]$data1[/blue] = [url=http://perldoc.perl.org/functions/sprintf.html][black][b]sprintf[/b][/black][/url] [red]"[/red][purple]%-2s[/purple][red]"[/red], [blue]$data[/blue][red];[/red]
	[black][b]print[/b][/black] [red]"[/red][purple][blue]$data1[/blue] [/purple][red]"[/red][red];[/red]
[red]}[/red]
[black][b]print[/b][/black] [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[olive][b]for[/b][/olive] [black][b]my[/b][/black] [blue]$id[/blue] [red]([/red][black][b]sort[/b][/black] [url=http://perldoc.perl.org/functions/keys.html][black][b]keys[/b][/black][/url] [blue]%out[/blue][red])[/red] [red]{[/red]
	[black][b]print[/b][/black] [red]"[/red][purple][blue]$id[/blue]  [/purple][red]"[/red][red];[/red]
	[olive][b]for[/b][/olive] [black][b]my[/b][/black] [blue]$data[/blue] [red]([/red][blue]@array[/blue][red])[/red] [red]{[/red]
		[black][b]print[/b][/black] [blue]$out[/blue][red]{[/red][blue]$id[/blue][red]}[/red][red]{[/red][blue]$data[/blue][red]}[/red] ? [red]"[/red][purple]1 [/purple][red]"[/red] : [red]"[/red][purple]0 [/purple][red]"[/red], [red]"[/red][purple] [/purple][red]"[/red][red];[/red]
	[red]}[/red]
	[black][b]print[/b][/black] [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
[/tt]

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
You might have to change how your reading your data in as mine is specific to reading it out of the hash in that format, but you should be able to figure that out.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
Many thanks travs,
I have read it from the file I have and it seems to be working fine.
This was of great help.
Mozart
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top