473,378 Members | 1,510 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,378 software developers and data experts.

How to slice range of array along a (a-priori unknown) axis?

Hello,

I am using the Numeric package, and I need to strip edge cells off an array
(dimension unknown) in an a-priori unknown direction. I implented this as
follows:

def el_remove_bcells(var, axis):
""" elementary routine to strip the edge cells of an array for the given
direction.
"""

if axis == 0:
return var[1:-1]
if axis == 1:
return var[:, 1:-1]
else:
return var[:, :,1:-1]

But this only works for at most 3D arrays. It must be possible to program
this fragment without the ifs, for arrays of arbitrary dimension. Is there
a command in the Numerical package I can use for this? It is very important
that this method is very fast, as my arrays normally are in the order of
100 Mb.

TIA, Maarten

--
================================================== =================
Maarten van Reeuwijk Thermal and Fluids Sciences
Phd student dept. of Multiscale Physics
www.ws.tn.tudelft.nl Delft University of Technology
Jul 18 '05 #1
3 1904
This should work.

def el_remove_bcells(var, axis):
if*axis*==*0:
**** return*var[1:-1]
else:
return eval("var[%s1:-1]" % ":," * axis)

--
Regards,

Diez B. Roggisch
Jul 18 '05 #2
> def el_remove_bcells(var, axis):
if*axis*==*0:
return*var[1:-1]
else:
return eval("var[%s1:-1]" % ":," * axis)


Thanks, that does the trick! I love the fact that you can create commands at
runtime :). I also found a command called swapaxes, which is very fast, so
just in case you're interested, here's another way:

def el_remove_bcells(var, axis):
swap = swapaxes(var, 0, axis)
swap = swap[1:-1]
return swapaxes(swap, 0, axis)

--
================================================== =================
Maarten van Reeuwijk Thermal and Fluids Sciences
Phd student dept. of Multiscale Physics
www.ws.tn.tudelft.nl Delft University of Technology
Jul 18 '05 #3
Maarten van Reeuwijk wrote:
I am using the Numeric package, and I need to strip edge cells off an
array (dimension unknown) in an a-priori unknown direction. I implented
this as follows:

def el_remove_bcells(var, axis):
""" elementary routine to strip the edge cells of an array for the
given
direction.
"""

if axis == 0:
return var[1:-1]
if axis == 1:
return var[:, 1:-1]
else:
return var[:, :,1:-1]

But this only works for at most 3D arrays. It must be possible to program
this fragment without the ifs, for arrays of arbitrary dimension. Is there
a command in the Numerical package I can use for this? It is very
important that this method is very fast, as my arrays normally are in the
order of 100 Mb.


Maybe:
def clipdim(a, n): .... t = [slice(None)] * N.rank(a)
.... t[n] = slice(1,-1)
.... return a[t]
.... a array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]]) clipdim(a, 0) array([ [[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]]]) clipdim(a, 1) array([[ [ 3, 4, 5]],
[ [12, 13, 14]],
[ [21, 22, 23]]]) clipdim(a, 2)

array([[[ 1],
[ 4],
[ 7]],
[[10],
[13],
[16]],
[[19],
[22],
[25]]])

Peter
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Michael Drumheller | last post by:
(If you're not interested in NumArray, please skip this message.) I am new to NumArray and I wonder if someone can help me with array-indexing. Here's the basic situation: Given a rank-2 array...
3
by: kr | last post by:
Hi there, Is it possible to slice an array from a particular element to the end of it without using $#<array name>? My goal is the get only the IP addresses from the output of gethostbyname(). ...
40
by: Ron Adam | last post by:
After considering several alternatives and trying out a few ideas with a modified list object Bengt Richter posted, (Thank You), I think I've found a way to make slice operation (especially far end...
26
by: Protoman | last post by:
How would you use a lookup table of char*, I mean, how would you find out where the intersection of D and E is? And how would you read two strings, and parse each character. Here's the array: ...
2
by: Louis | last post by:
What I am trying to do seems simple, but I haven't figured out how to do it. I need to add the contents of the cells in one array, subject to the values in another array. Normally I would utilize...
5
by: Richard Delorme | last post by:
The n869 draft says: J.2 Undefined behavior The behavior is undefined in the following circumstances: -- An array subscript is out of range, even if an object is ...
3
by: darrel | last post by:
I'm trying to create an array with a size I don't know. For instance: Dim values() As Integer Dim valuesString As String = "" while something valuesString = valuesString & newValue 7 ", "...
1
by: Richard Harter | last post by:
On Fri, 27 Jun 2008 09:28:56 -0700 (PDT), pereges <Broli00@gmail.comwrote: There are some obvious questions that should be asked, e.g., is the contents of your array already sorted as your...
4
by: omono84 | last post by:
I know that this should be rather simple but i seem to be missing a step to get it to work. and have been unable to find a solution on the net. The aim is that I click on the open button to find...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.