[SciPy-User] [SciPy-user] ValueError: arrays must have same number of dimensions

surfcast23 surfcast23 at gmail.com
Thu Jun 28 12:49:41 EDT 2012


Hi sanGuziy,

  That did it. Thank you! can you explain why it worked for future
reference?

Khary



sanGuziy wrote:
> 
> Try this
> 
> data = np.concatenate((v.reshape((512,1)).transpose(), k))
> 
> Cheers
> --
> Oleksandr Huziy
> 
> 2012/6/28 surfcast23 <surfcast23 at gmail.com>
> 
>>
>> Hi Tony,
>>
>>  I tried you r translation and it works, but I still get the error
>>
>>  line 50, in <module>     data = np.concatenate((v, k),axis = 0);
>> ValueError: arrays must have same number of dimensions
>>
>> Thanks,
>> Khary
>>
>>
>>
>> Tony Yu-3 wrote:
>> >
>> > On Thu, Jun 28, 2012 at 11:48 AM, Jorge E. ´Sanchez Sanchez <
>> > hnry2k at hotmail.com> wrote:
>> >
>> >>  Hi,
>> >>
>> >> it seems to me that you don't know that loops in python end before the
>> >> last index value of the loop,
>> >> you just need to do something like:
>> >>  Np1 = N+1
>> >> for j in range (1, Np1):
>> >>
>> >> HTH
>> >> Best Regards
>> >> Jorge
>> >>
>> >
>> >>
>> >> > Date: Thu, 28 Jun 2012 08:32:47 -0700
>> >> > From: surfcast23 at gmail.com
>> >> > To: scipy-user at scipy.org
>> >> > Subject: [SciPy-User] [SciPy-user] ValueError: arrays must have same
>> >> number of dimensions
>> >>
>> >> >
>> >> >
>> >> > Hi I am trying to translate a Matlab code from Trefethen's Spectral
>> >> Methods
>> >> > in MATLAB to Python. I am running into a problem with
>> numpy.concatenate
>> >> > wanting the arrays to have the same number of dimensions.
>> >> > Here is part of the Matlab code that I am rewriting
>> >> >
>> >> >
>> >> > N = 512; h = 2*pi/N; x = h*(1:N); t = 0; dt = h/4;
>> >> > a = .1;
>> >> > c = a + sin (x-1).^2;
>> >> > v = exp(-100*(x-1).^2); vold = exp(-100*(x-a*dt-1).^2);
>> >> >
>> >> > column = [0 .5*(-1).^(1:N-1).*cot((1:N-1)*h/2)];
>> >> > D = toeplitz(column,-column);
>> >> >
>> >> > % Time-stepping by leap frog formula:
>> >> > tmax = 15; tplot = .15; clf, drawnow, set(gcf,'renderer','zbuffer')
>> >> > plotgap = round(tplot/dt); dt = tplot/plotgap;
>> >> > nplots = round(tmax/tplot);
>> >> > data = [v; zeros(nplots,N)]; tdata = t;
>> >> >
>> >> >
>> >> > What I am trying in Python
>> >> >
>> >> > for j in range(1,N):
>> >>
>> >
>> > [snip]
>> >
>> > You actually don't need to loop. Your original matlab code translates
>> > fairly naturally into numpy code: the main difference being the
>> > substitution of `np.arange(N) + 1` for `[1:N]`, where `np.arange(N)`
>> gives
>> > values from 0 to N - 1, as Jorge suggests (although it's not specific
>> to
>> > loops).
>> >
>> > Here's a rough translation (not tested against matlab output, so I
>> could
>> > have made some errors in the translation).
>> >
>> > Cheers,
>> > -Tony
>> >
>> > #~~~ code
>> > import numpy as np
>> > from scipy.linalg import toeplitz
>> > from scipy.special import cotdg
>> >
>> > N = 512
>> > h = 2 * np.pi/N
>> > x = h * (np.arange(N) + 1)
>> > t = 0
>> > dt = h / 4
>> > a = .1
>> > c = a + np.sin(x - 1)**2
>> > v = np.exp(-100 * (x - 1)**2)
>> > vold = np.exp(-100 * (x - a*dt - 1)**2)
>> >
>> > i = np.arange(1, N)
>> > column = np.hstack([0, .5 * (-1**i) * cotdg(i * h/2)])
>> > D = toeplitz(column, -column)
>> >
>> > _______________________________________________
>> > SciPy-User mailing list
>> > SciPy-User at scipy.org
>> > http://mail.scipy.org/mailman/listinfo/scipy-user
>> >
>> >
>>
>> --
>> View this message in context:
>> http://old.nabble.com/ValueError%3A-arrays-must-have-same-number-of-dimensions-tp34086886p34087216.html
>> Sent from the Scipy-User mailing list archive at Nabble.com.
>>
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User at scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
> 
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
> 
> 

-- 
View this message in context: http://old.nabble.com/ValueError%3A-arrays-must-have-same-number-of-dimensions-tp34086886p34087320.html
Sent from the Scipy-User mailing list archive at Nabble.com.




More information about the SciPy-User mailing list