1

hii experts i have a 1d numpy array and i want to repeat it 3 times vertically,

my_input_array = [-1.02295637 -0.60583836 -0.42240581 -0.78376377 -0.85821456]

i tried the below code

import numpy as np
x=np.loadtxt(my_input_array)
x.concatenate()

however i get error...in this way...hope i will get some solution.Thanks.

my expected output should be as below

 -1.02295637
 -0.60583836
 -0.42240581
 -0.78376377
 -0.85821456
 -1.02295637
 -0.60583836
 -0.42240581
 -0.78376377
 -0.85821456
 -1.02295637
 -0.60583836
 -0.42240581
 -0.78376377
 -0.85821456
 
10
  • And the error is ? Commented Sep 25, 2020 at 21:24
  • concatenation problem... Commented Sep 25, 2020 at 21:25
  • 1
    That is not the error given by the code Commented Sep 25, 2020 at 21:25
  • x.concatenate() - where did you get the idea to do this? It's not a documented method, is it? You'll have a lot of problems if you make wild guesses without checking documentation. That said, you might find np.tile to be useful - look it up. Commented Sep 25, 2020 at 21:26
  • numpy.org/doc/stable/reference/generated/numpy.concatenate.html Commented Sep 25, 2020 at 21:27

3 Answers 3

2

numpy.tile

np.tile(my_input_array, 3)

Output

array([-1.02295637, -0.60583836, -0.42240581, -0.78376377, -0.85821456,
       -1.02295637, -0.60583836, -0.42240581, -0.78376377, -0.85821456,
       -1.02295637, -0.60583836, -0.42240581, -0.78376377, -0.85821456])

Edit: just noticed @hpaulj's answer. I'll still leave my answer but he mentioned np.tile first.

Sign up to request clarification or add additional context in comments.

Comments

1

Just use tile method which multiplies the array with given shape and reshape method to structure it. Use x.shape[0]*x.shape[1] to change it into a column vector without explicitly giving the shape dimensions!

x=np.tile(x,(3,1))
y=x.reshape(x.shape[0]*x.shape[1])

3 Comments

i donot need any reshape...it should be automatic without reshaping
Does it help now?
glad to help.. I just think you should use this technique as the for loop will only print array but the structure as column vector is really important to manipulate data inside!
0

This is what you want:

x=np.concatenate([my_input_array, my_input_array, my_input_array])
for i in x:
    print(i)

Output:

-1.02295637
-0.60583836
-0.42240581
-0.78376377
-0.85821456
-1.02295637
-0.60583836
-0.42240581
-0.78376377
-0.85821456
-1.02295637
-0.60583836
-0.42240581
-0.78376377
-0.85821456

4 Comments

but how to arrange the output like my expected result...i donot need array again
Alternatively, np.concatenate([my_input_array]*3)
That's the way numpy arrays print by default. If you need specific formatting, then you have to do the formatting. for f in my_array: ` print(f)`
Of course it does. You need to provide some of the brains yourself. I assume you simply typed in my command, without actually creating the array. my_array = np.concatenate([my_input_array]*3)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.