[SciPy-user] concatenation of arrays woes !!

Karthikesh Raju karthik at james.hut.fi
Tue May 4 12:19:55 EDT 2004


Hi Gary and others,

thankx for your reply. i had managed to break the procedure into bits and
pieces and get the work done. i have attached the file. Is there some way
by which we can efficiently do something like

s2 = s[0:CL-d[k,l],k]
s3 = concatenate(z1,z2,s2[:,NewAxis])
stemp[:,NewAxis] = s3

is there any means by which i can avoid having s2 as a variable, and by
using the NewAxis during striding process itself.

i have just built this matrix now, row by row :), any way to improve this!

With warm regards

karthik


-----------------------------------------------------------------------
Karthikesh Raju,		    email: karthik at james.hut.fi
Researcher,			    http://www.cis.hut.fi/karthik
Helsinki University of Technology,  Tel: +358-9-451 5389
Laboratory of Comp. & Info. Sc.,    Fax: +358-9-451 3277
Department of Computer Sc.,
P.O Box 5400, FIN 02015 HUT,
Espoo, FINLAND
-----------------------------------------------------------------------

On Tue, 4 May 2004, Gary Pajer wrote:

> ----- Original Message -----
> From: "Karthikesh Raju" <karthik at james.hut.fi>
> To: <scipy-user at scipy.net>
> Sent: Tuesday, May 04, 2004 4:10 AM
> Subject: [SciPy-user] concatenation of arrays woes !!
>
>
> >
> > Hi All,
> >
> > is it possible to concatenate arrays along the column,
> >
> > A[:,1:5] = concatenate(a,b,c)
> > a is 2x1, b 2x1, c 1x1
> >
> > i have been trying to convert the following matlab code and this is my
> > stumbling block for several months :(
> >
> > K = 3
> > CL = 5
> > L = 2
> > d -- (K,L) in the range (0,CL)
> > codes -- (K,CL)
> > window = 1
> > s = codes'
> >
> > output S -(CL*window,K*L*(window+1)) matrix
> >
> > for k = 1:K
> > for l = 1:L
> >     S(:,1+(window+1)*(l-1)+(window+1)*L*(k-1)) =
> >            [s(CL-d(k,l)+1:CL,k);
> zeros(CL*(window-1),1);zeros(CL-d(k,l),1)]
> >     S(:,(window+1)*1+(window+1)*L*(k-1)) =
> >            [zeros(d(k,l),1);zeros(CL*(window-1),1);s(1:CL-d(k,l),k)]
> >     for n=2:window
> >         S(:,n+(window+1)*(l-1)+(numbits+1)*L*(k-1)) =
> >            [zeros(d(k,l),1); zeros(CL*(n-2),1);
> > s(:,k);zeros((window-n)*CL+CL-d(k,l),1)]
> >     end
> > end
> > end
> >
> > --------------------
> >
> > This module has been my stumbling block for the last 2 months, had no
> > problems with matlab, here i have all kinds of concatination woes. Please
> > give me pointers on how to achieve such concatenations,
> >
> > thanks a lot
> >
> > regards
> > karthik
>
>
>
> I'm still not sure what you are trying to achieve.  If you post a minimal
> contrived example of what you are trying to do instead of a clip from the
> middle of your code it would be easier to figure out.
>
> But have you explored r_[ ]  and c_[ ] ?   ('c' for column, 'r' for row)
> These constructions are designed to aid concatenation:
>
> >>> from scipy import *
> >>> x=array([1,2])
> >>> y=array([3,4])
>
> >>> c_[x,y]
> array([1, 2, 3, 4])
>
> >>> c_[x[:,NewAxis],y[:,NewAxis]]
> array([[1, 3],
>        [2, 4]])
>
> >>> r_[x[:,NewAxis],y[:,NewAxis]]
> array([[1],
>        [2],
>        [3],
>        [4]])
>
>
> hth,
> gary
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.net
> http://www.scipy.net/mailman/listinfo/scipy-user
>
>
-------------- next part --------------
from numarray import *

K = 3
L = 2
CL = 5

codes = arange(1,16,shape=(K,CL))
codes.transpose()
s = array(codes, copy=1)
print s
d = ones((K,L))
window = 1

S = zeros([CL*window,K*L*(window+1)])

for k in range(K):
	for l in range(L):
		print "--"*30 + "\n"
		
		# first row
		#--------------------------------------------------
		stemp = S[:,(window+1)*(l)+(window+1)*L*(k)] 
		s2 =  s[CL-d[k,l]:CL,k]
		z1 =  zeros([CL*(window-1),1])
		z2 =  zeros([CL-d[k,l],1])
		if CL*(window-1) == 0:
		 	s3 = concatenate((s2[:,NewAxis],z2))
		else:
		 	s3 = concatenate((s2[:,NewAxis],z1,z2))	
		stemp[:,NewAxis] = s3
		#-------------------------------------------------- 


		# # second row
		#-------------------------------------------------- 
		stemp = S[:,(window+1)*l+window+(window+1)*L*k]
		z1 = zeros([d[k,l],1]);
		z2 = zeros([CL*(window-1),1])
		s2 = s[0:CL-d[k,l],k]
		if CL*(window-1) == 0:
			s3 = concatenate((z1,s2[:,NewAxis]))
		else:
			s3 = concatenate((z1,z2,s2[:,NewAxis]))
		stemp[:,NewAxis] = s3
		#-------------------------------------------------- 

		# final push
		for w in range(0,window-1):
			stemp = S[:,w+1+(window+1)*l+(window+1)*L*k]
			z1 = zeros([d[k,l],1])
			z2 = zeros([CL*w,1])
			s2 = s[:,k]
			z3 = zeros([(window-(w+2))*CL+CL-d[k,l],1])
			if CL*w == 0:
				s3 = concatenate((z1,s2[:,NewAxis],z3))
			else:
				s3 = concatenate((z1,z2,s2[:,NewAxis],z3))
			print  s3
			stemp[:,NewAxis] = s3
print S
print S.shape
		

		
		
		



More information about the SciPy-User mailing list