[Numpy-discussion] Some help on matlab to numpy translation

Ryan May rmay31@gmail....
Sat Mar 13 08:51:29 CST 2010


On Sat, Mar 13, 2010 at 4:45 AM, Fabrice Silva <silva@lma.cnrs-mrs.fr> wrote:
> Le samedi 13 mars 2010 à 10:20 +0100, Nicolas Rougier a écrit :
>> Hello,
>> I'm trying to translate a small matlab program for the simulation in a
>> 2D flow in a channel past a cylinder and since I do not have matlab
>> access, I would like to know if someone can help me, especially on
>> array indexing. The matlab source code is available at:
>> http://www.lbmethod.org/openlb/lb.examples.html and below is what I've
>> done so far in my translation effort.
>>
>> In the matlab code, there is a "ux" array of shape (1,lx,ly) and I do
>> not understand syntax: "ux(:,1,col)" with "col = [2:(ly-1)]". If
>> someone knows, that would help me a lot...
>
>
> As ux 's shape is (1,lx,ly), ux(:,1,col) is equal to ux(1,1,col) which
> is a vector with the elements [ux(1,1,2), ... ux(1,1,ly-1)].
> Using ":" juste after the reshape seems a lit bit silly...

Except that python uses 0-based indexing and does not include the last
number in a slice, while Matlab uses 1-based indexing and includes the
last number, so really:
    ux(:,1,col)
becomes:
    ux(0, 0, col) # or ux(:, 0, col)

And if col is
    col = [2:(ly-1)]
This needs to be:
    col = np.arange([1, ly - 1)

Ryan

-- 
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma


More information about the NumPy-Discussion mailing list