0

I have an array with size=(100,600,600) and want to split it into 4 (25,600,600) arrays and save them with names ldx1, ldx2, ldx3 and, ldx4 in n folders like below.

folder 1: ldx1, ldx2, ldx3 and, ldx4

folder 2: ldx1, ldx2, ldx3 and, ldx4

folder n: ldx1, ldx2, ldx3 and, ldx4

My code just saves the first file which is ldx1 in all folders. See my try below:


import numpy as np
import scipy.io

num_folders = 10
count=0

for i in range(num_folders):
   x=np.random.randint(0,1,size=(100,600,600))
   x_ = x[i:i+25,:]    
   count+=1
   scipy.io.savemat('fig/%d/ldx%d.mat' % (i,count),  mdict={'my_list':x_},do_compression=True) 
1
  • 1
    Why don't you first reshape your array to (4, 25, 600, 600) then loop over that? Commented Feb 3, 2022 at 18:43

1 Answer 1

2

You'll need two loops:

import numpy as np
import scipy.io

num_folders = 10

for i in range(num_folders):
   x = np.random.randint(0,1,size=(100,600,600))
   for j in range(4):
       scipy.io.savemat(
           'fig/%d/ldx%d.mat' % (i,j),
           mdict={'my_list':x[j*25:j*25+25,:,:]},
           do_compression=True
        ) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.