1

I have an numpy array of shape 24576x25 and i want to extract 3 array out of it. Where the first array contains the every 1st,4th,7th,10th,... element while second array contains 2nd,5,8,11th,... element and third array with 3rd,6,9,12th,... The output array sizes would be 8192x25. I was doing the following in MATLAB

c = reshape(a,1,[]);
x = c(:,1:3:end);
y = c(:,2:3:end);
z = c(:,3:3:end);

I have tried a[:,0::3] in python but this works only if i have array of shape divisible by 3. What can i do?

X,Y = np.mgrid[0:24576:1, 0:25:1]
a = X[:,::,3]
b = X[:,1::3]
c = X[:,2::3]

does not work either. I need a,b,c.shape = 8192x25

5
  • a[:, ::3] should be ok. Is a a numpy.ndarray ? Commented Jul 7, 2020 at 11:17
  • @paime doesn't work and yes it is a ndarray Commented Jul 7, 2020 at 11:22
  • I need x.shape , y.shape and z.shape = 8192x25 Commented Jul 7, 2020 at 11:23
  • If you need 8192x25, then it is a[0::3]. @bigbounty answer definitely works. Commented Jul 7, 2020 at 11:38
  • it gives me the same size but the elements picking is wrong.. as it should return 0,3,6,9,,... but its giving me 0 1 2 3,... Commented Jul 7, 2020 at 12:35

2 Answers 2

2

A simple tweak to your original attempt should yield the results you want:

X,Y = np.mgrid[0:24576:1, 0:25:1]
a = X[0::3,:]
b = X[1::3,:]
c = X[2::3,:]
Sign up to request clarification or add additional context in comments.

Comments

0
import numpy as np
a = np.arange(24576*25).reshape((24576,25))
a[::3]

a[::3].shape gives you (8192, 25)

3 Comments

i did the same, doesn't return 8192x25 x3
this gives me (8192,25) but not the correct elements as it should give 0,3,6,.. but I am not getting it
@AR. When you go through the elements of a[::3] - it will give you 0,3,6

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.