Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #31716 > unrolled thread
| Started by | inshu chauhan <insideshoes@gmail.com> |
|---|---|
| First post | 2012-10-19 10:40 +0200 |
| Last post | 2012-10-21 01:09 -0700 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.python
pls help me with this prog inshu chauhan <insideshoes@gmail.com> - 2012-10-19 10:40 +0200
Re: pls help me with this prog rusi <rustompmody@gmail.com> - 2012-10-19 06:06 -0700
Re: pls help me with this prog Tim Roberts <timr@probo.com> - 2012-10-20 19:33 -0700
Re: pls help me with this prog 88888 Dihedral <dihedral88888@googlemail.com> - 2012-10-21 01:09 -0700
Re: pls help me with this prog 88888 Dihedral <dihedral88888@googlemail.com> - 2012-10-21 01:09 -0700
| From | inshu chauhan <insideshoes@gmail.com> |
|---|---|
| Date | 2012-10-19 10:40 +0200 |
| Subject | pls help me with this prog |
| Message-ID | <mailman.2495.1350636042.27098.python-list@python.org> |
in this prog I have written a code to calculate teh centre of a given 3D data..
but i want to calculate it for every 3 points not the whole data, but
instead of giving me centre for every 3 data the prog is printing the
centre 3 times...
import cv
from math import floor, sqrt, ceil
from numpy import array, dot, subtract, add, linalg as lin
def CalcCentre(data):
centre = array([0,0,0])
count = 0
n = 0
for p in data[n:n+3]:
centre = add(centre, array(p[:3]))
count += 1
centre = dot(1./count, centre)
return centre
n += 1
def ReadPointCloud(filename):
f = open(filename)
result = []
for l in f:
sp = l.split()
t = tuple(map(float, sp[1:4]))
result.append(t)
return result
def main (data):
j = 0
for i in data[:3]:
while j != 3:
centre = CalcCentre(data)
j += 1
print centre
if __name__ == '__main__':
data = ReadPointCloud(r'Z:\data\NEHreflectance_Scanner 1_part.txt')
main(data)
PLS HELP ;;;;
[toc] | [next] | [standalone]
| From | rusi <rustompmody@gmail.com> |
|---|---|
| Date | 2012-10-19 06:06 -0700 |
| Message-ID | <6cfb7fec-b53f-44c3-adef-57bad7adf918@jj5g2000pbc.googlegroups.com> |
| In reply to | #31716 |
Dont know what your code does/tries to do. Anyway some points: On Oct 19, 1:40 pm, inshu chauhan <insidesh...@gmail.com> wrote: > in this prog I have written a code to calculate teh centre of a given 3D data.. > > but i want to calculate it for every 3 points not the whole data, but > instead of giving me centre for every 3 data the prog is printing the > centre 3 times... > > import cv > from math import floor, sqrt, ceil > from numpy import array, dot, subtract, add, linalg as lin > > def CalcCentre(data): > centre = array([0,0,0]) > count = 0 > n = 0 > for p in data[n:n+3]: > centre = add(centre, array(p[:3])) > count += 1 > centre = dot(1./count, centre) > return centre > n += 1 The return makes the for iterate only once And n += 1 will never be reached > t = tuple(map(float, sp[1:4])) drops the sp[0] element.
[toc] | [prev] | [next] | [standalone]
| From | Tim Roberts <timr@probo.com> |
|---|---|
| Date | 2012-10-20 19:33 -0700 |
| Message-ID | <o8n688t7q03059mbebt5434h7ki77pnovv@4ax.com> |
| In reply to | #31716 |
inshu chauhan <insideshoes@gmail.com> wrote:
>
>but i want to calculate it for every 3 points not the whole data, but
>instead of giving me centre for every 3 data the prog is printing the
>centre 3 times...
>
>def main (data):
> j = 0
> for i in data[:3]:
> while j != 3:
> centre = CalcCentre(data)
> j += 1
> print centre
This loop doesn't do what you think it does. Think about the problem you
have to solve. You have a set of data. You want to operate on the whole
set, 3 points at a time. What this loop does is grab the first three
points (only), then calculates the center on the entire list three times.
You want something like:
# As long as there is data in the list:
while data:
# Split the list into two parts: first 3 and the rest.
first3, data = data[3:],data[3:]
# Operate on the first three.
print CalcCenter( first3 )
To make it a little more resilient, you might make sure that len(data) is a
multiple of 3, or at least stop when it is shorter than 3.
To be more clever, you can write a function that returns 3 at a time:
def N_at_a_time( iter, n ):
while len(iter) >= n:
yield iter[:n]
iter = iter[n:]
Now you can do this:
def main(data):
for first3 in N_at_a_time(data, 3):
print CalcCenter(first3)
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2012-10-21 01:09 -0700 |
| Message-ID | <700c410d-327e-4325-892f-4841825946d8@googlegroups.com> |
| In reply to | #31716 |
On Friday, October 19, 2012 4:40:42 PM UTC+8, inshu chauhan wrote: > in this prog I have written a code to calculate teh centre of a given 3D data.. > > > > but i want to calculate it for every 3 points not the whole data, but > > instead of giving me centre for every 3 data the prog is printing the > > centre 3 times... > > > > import cv > > from math import floor, sqrt, ceil > > from numpy import array, dot, subtract, add, linalg as lin > > > > > > > > > > def CalcCentre(data): > > centre = array([0,0,0]) > > count = 0 > > n = 0 > > for p in data[n:n+3]: > > centre = add(centre, array(p[:3])) > > count += 1 > > centre = dot(1./count, centre) > > return centre > > n += 1 > > def ReadPointCloud(filename): > > f = open(filename) > > result = [] > > for l in f: > > sp = l.split() > > t = tuple(map(float, sp[1:4])) > > result.append(t) > > return result > > > > def main (data): > > > > > > j = 0 > > for i in data[:3]: > > while j != 3: > > centre = CalcCentre(data) > > j += 1 > > print centre > > > > > > if __name__ == '__main__': > > data = ReadPointCloud(r'Z:\data\NEHreflectance_Scanner 1_part.txt') > > > > main(data) > > > > > > > > > > PLS HELP ;;;; # assume data is a list of 3 n numbers, n!=0 n3=data.length() n=n/3 x=sum(data[0:n3:3])/n y=sum(data[1:n3:3])/n z=sum(data[2:n3:3])/n #(x,y,z)
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2012-10-21 01:09 -0700 |
| Message-ID | <mailman.2580.1350806993.27098.python-list@python.org> |
| In reply to | #31716 |
On Friday, October 19, 2012 4:40:42 PM UTC+8, inshu chauhan wrote: > in this prog I have written a code to calculate teh centre of a given 3D data.. > > > > but i want to calculate it for every 3 points not the whole data, but > > instead of giving me centre for every 3 data the prog is printing the > > centre 3 times... > > > > import cv > > from math import floor, sqrt, ceil > > from numpy import array, dot, subtract, add, linalg as lin > > > > > > > > > > def CalcCentre(data): > > centre = array([0,0,0]) > > count = 0 > > n = 0 > > for p in data[n:n+3]: > > centre = add(centre, array(p[:3])) > > count += 1 > > centre = dot(1./count, centre) > > return centre > > n += 1 > > def ReadPointCloud(filename): > > f = open(filename) > > result = [] > > for l in f: > > sp = l.split() > > t = tuple(map(float, sp[1:4])) > > result.append(t) > > return result > > > > def main (data): > > > > > > j = 0 > > for i in data[:3]: > > while j != 3: > > centre = CalcCentre(data) > > j += 1 > > print centre > > > > > > if __name__ == '__main__': > > data = ReadPointCloud(r'Z:\data\NEHreflectance_Scanner 1_part.txt') > > > > main(data) > > > > > > > > > > PLS HELP ;;;; # assume data is a list of 3 n numbers, n!=0 n3=data.length() n=n/3 x=sum(data[0:n3:3])/n y=sum(data[1:n3:3])/n z=sum(data[2:n3:3])/n #(x,y,z)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web