I want to know the dimensions of several matrices, which I read from a file.
I have done the code to calculate the number of rows and total number of elements of the matrix, and know, thus, the number of columns, dividing the total number of elements among the number of rows.
program matrix
implicit none
integer,dimension(:),allocatable::total
integer row,io,countRows,columns,m,numElements
open(12,file='matrix.txt',status='old',iostat=io)
if (io.ne.0) then
write(*,*)'error to open file'
stop
end if
!count rows
countRows=0
io=0
do while (io.ge.0)
read(12,*,iostat=io) row
if (io.eq.0) countRows=countRows+1
end do
rewind(12)
!total of elements
io=0
do m=1,1000
allocate(total(m))
read(12,*,iostat=io) total
deallocate(total)
rewind(12)
if (io.ne.0) exit
end do
numElements=size(total)-1
columns=numElements/countRows
close(12)
end program
The problem is that it only works when there is a matrix in the file, because if there are several, separated by one or several blank lines, it tells me the number of total rows and number of total elements of the file.
I need to know how to separate those matrices to count their rows and columns independently.
Sample file would may look like
90 21 11 13
12 11 10 11
33 44 76 55
12 12
87 99
33 12 17
45 98 77
I can propose a solution like this, not a fancy one, but seemed to work with the example:
program matrix
implicit none
integer,dimension(:),allocatable::total
integer row,io,countRows,columns,m,numElements,icol
integer mlocs(100,2),imat,countMat,countSpace,nmats
character(len=256) line
character linechar
mlocs=0;
open(12,file='matrix.txt',status='old',iostat=io)
if (io.ne.0) then
write(*,*)'error to open file'
stop
end if
!count rows
countRows=0
countMat=1;countSpace=0;
do while (1.gt.0.0)
read(12,'(a)',end=100) line
print*,trim(line)
countRows=countRows+1;countSpace=1;
if (len(trim(line)).eq.0) then
countMat=countMat+1;mlocs(countMat,1)=countRows;
elseif (len(trim(line)).gt.0) then
do icol=1,len(trim(line))
read(line(icol:icol),'(a)') linechar
if (linechar.eq.' ') then
countSpace=countSpace+1;
mlocs(countMat,2)=countSpace
endif
enddo
endif
end do
100 print*,'Number of lines is ',countRows
print*,'Number of matrices is ',countMat
nmats=countMat;
do imat=1,nmats
print*,'NUmber of elements in matrix ',imat,' is ',mlocs(imat,2:2)
enddo
close(12)
end program
Related
I have a large character with a length of 4800. The elements in the vector are not in the correct order so I would like to change that.
The order should be the first 96 elements, followed by the third 96 elements, followed by the fifth 96 elements, seventh 96 elements,... until the last 96 elements. Then it should be followed by the second 96 elements, the fourth 96 elements, the sixth 96 elements, .... until the last 96. So basically, extract 96 elements with a step of 96 until the end of the character.
So I could do it like this:
icfinal[c(1:96,193:289,386:481,...)]
But this will take me a long time. Does anyone know how to solve this with one function?
Much appreciated!
According to your logic, if you need the first 96, then the third then the fifth we could do something like:
icfinal[outer(1:96, 96 * seq(0, 4800/96, 2), "+")]
I have a vector modbirths which has 23 entries.
I have a vector pop_vars_modded which has 23 entries.
I want to synchronize my for loop so that county[i] and pop_vars_modded[i]
move at the same speed. For example, the loop would only do county[1] and pop_vars_modded[1], ..... county[19] and pop_vars_modded[19], but not county[7] and pop_vars_modded[22], or other values that are not synchronous. Here is the code I am working with:
for(county in modbirths){
for( i in 1:23){
print(dbinom(county[i], pop_vars_modded[i], p=wyo2004_smokerate, log=T))
}}
the output gives me 23^2 = 529 numbers that dbinom produces (every combination of the 23 possible values i takes on), but I only want the key 23 ones.
I'm trying to come up with a way to generate a grid/level from a seed.
I'm thinking of using a 6 x 6 grid with 3, possible tiles in each.
The seed for that would be 108 characters long, i doubt anyone would want to copy 108 char long seed. (the game will probably be on iPhone, so the seed will be entered via keyboard)
Anyways of shortening it?
I thought a fun way would be to use words.
Separate the the grid into three 2 by 6 lines. and have each line represented by an english word.
The player would then just type in 3 words which is a lot easier & gives a kind of name to the level.
Any thoughts on how i could achieve this?
(I'm currently using gamesalad)
Thank you for your time,
Jordan
Shorting the seed.
Well 108 characters is a little over kill. You have 6 by 6 cell so that's 36 cells total. Each cell has 3 different states yet a char has at min 64 different states (A-Z,a-z,0-9, and a few punctuations to keep it simple) So even with 36 character we have way more than we need.
So lets break it down some more. Computers work in binary. A bit is the smallest bit of information it can use. One bit can be on or off so it has 2 possible states. 2 bits can be off,off or off,on or on,off or on,on so that is 4 different states. If we add another bit we get two more states for every previous state so that is 4*2 = 8 states for 3 bits. Times by 2 again for a 4th bit we get 16 states, again for 32 and again for 64 which just happens to be the number of characters we want to use. So it takes 6 bits to store 64 possible states.
Now lets look at the grid. Each cell has 3 states each. So two cells have 9 possible combination 3*3 and for 3 cells its 3*3*3 = 27 next is 3*3*3*3 = 81 or 3 to the power of 4 cells or 3^4 = 81. We can use this trend to work out how many possible combinations there are for all 36 cells. 3 to the power of 36. That is a big number 150,094,635,296,999,121 or One hundred and fifty million billion combinations.
So how many bits do we need to store that number. I could cheat and ask the calculator but that's too easy,
Let's see, 3 cells have 27 combinations and 5 bits have 32. so 3 cells go into 36 cell 12 times and we have 5 bits for every 3 cells so 5*12 is 60bits. That's a nice round number as our chars are 6 bits each we can shove every possible grid combination into 10 char with some to spare. We have 5 spare combinations for every 3 cells so we have 60 combinations spare 12*5. Darn if only we had 4 more combinations that would be 64 which is 2^6 or 6 bits or one char. We can not take away half a character so 10 characters it has to be. BTW 4 combinations is two bits so take that from our 60 char bits and the minimum number of bits to store your grid is 58. 2 to the power of 58 should be just bigger than 3 to the power of 36.
Thus your grid requires 10 chars. Each being any one of ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789:) perfect (8 bytes 4 spare bits)
I pick my first 10 char seed BLINDMAN67 now what?
I have an array of numbers (called tails.Z) ranging from 0 to 999 and I want to see which numbers appear most frequently. In order to do so, I do a simple frequency plot using hist(tails.Z, breaks=1000) with the following result:
According to the plot, the most frequent number appears over 400 times and is some value close to zero. A second peak is somewhere around the value of 200 and indicates that the number appears just short of 400 times.
However, when I do sort(table(tails.Z)) to see the actual numbers and their frequencies I get that the most frequent number is 175 which appears 377 times, then the 2nd most frequent number is 176 which appears 290 times, then 3 which appears 266 times, 0 255 times and 5 263 times. How is it possible that the first peak in the graph is higher than 400 but in table there is no number with that frequency?
EDIT: I should add that tails.Z is an array of integers ranging from 0 to 999 and that there is 114,411 elements in it.
See what hist function does using str:
hs=hist(tails.Z, breaks=1000)
str(hs)
tail(cbind(hs$mids,hs$counts),20)
barplot(hs$counts)
summary(hs$counts)
This is more of a maths question than programming but I figure a lot of people here are pretty good at maths! :)
My question is: Given a 9 x 9 grid (81 cells) that must contain the numbers 1 to 9 each exactly 9 times, how many different grids can be produced. The order of the numbers doesn't matter, for example the first row could contain nine 1's etc. This is related to Sudoku and we know the number of valid Sudoku grids is 6.67×10^21, so since my problem isn't constrained like Sudoku by having to have each of the 9 numbers in each row, column and box then the answer should be greater than 6.67×10^21.
My first thought was that the answer is 81! however on further reflection this assumes that the 81 numbers possible for each cell are different, distinct number. They are not, there are 81 possible numbers for each cell but only 9 possible different numbers.
My next thought was then that each of the cells in the first row can be any number between 1 and 9. If by chance the first row happened to be all the same number, say all 1s, then each cell in the second row could only have 8 possibilites, 2-9. If this continued down until the last row then number of different permutations could be calculated by 9^2 * 8^2 * 7^2 ..... * 1^2. However this doesn't work if each row doesn't contain 9 of the same number.
It's been quite a while since I studied this stuff and I can't think of a way to work it out, I'd appreciate any help anyone can offer.
Imagine taking 81 blank slips of paper and writing a number from 1 to 9 on each slip (nine of each number). Shuffle the deck, and start placing the slips on the 9x9 grid.
You'd be able to create 81! different patterns if you considered each slip to be unique.
But instead you want to consider all the 1's to be equivalent.
For any particular configuration, how many times will that configuration be repeated
due to the 1's all being equivalent? The answer is 9!, the number of ways you can permute the nine slips with 1 written on them.
So that cuts the total number of permutations down to 81!/9!. (You divide by the number of indistinguishable permutations. Instead of 9! indistinguishable permutations, imagine there were just 2 indistinguishable permutations. You would divide the count by 2, right? So the rule is, you divide by the number of indistinguishable permutations.)
Ah, but you also want the 2's to be equivalent, and the 3's, and so forth.
By the same reasoning, that cuts down the number of permutations to
81!/(9!)^9
By Stirling's approximation, that is roughly 5.8 * 10^70.
First, let's start with 81 numbers, 1 through 81. The number of permutations for that is 81P81, or 81!. Simple enough.
However, we have nine 1s, which can be arranged in 9! indistinguishable permutations. Same with 2, 3, etc.
So what we have is the total number of board permutations divided by all the indistinguishable permutations of all numbers, or 81! / (9! ** 9).
>>> reduce(operator.mul, range(1,82))/(reduce(operator.mul, range(1, 10))**9)
53130688706387569792052442448845648519471103327391407016237760000000000L