I have one 5D data matrix containing values (:,:,1,1,1), (:,:,1,1,2),.....(:,:,1,1,10) with 57*92 elements each. I want to split this matrix into 10 matrices containing values (:,:,1,1,1) 57*92 in first matrix, (:,:,1,1,2) 57*92 in another matrix and so on. I am using Matlab.

The file looks as (have copied little part of it):

val(:,:,1,1,1) =

  Columns 1 through 11

      0      0      0      0      0      0      0      0      0      0      0
      0      0      0      0      0      0      0      0      0      0      0
      0      0      0      0      0      0      0      0      0      0      0
  14637      0      0      0      0      0      0      0      0      0      0
      0      0      0      0      0      0      0      0      0      0      0
      0      0      0      0      0      0      0      0      0      0      0
      0      0  14637  14637      0      0      0      0      0      0      0
  14638  14608  14621  14637  14637      0      0      0      0      0      0
  14653  14617  14608  14608      0      0      0      0      0      0      0
      0      0      0      0      0      0      0      0      0      0      0
      0      0      0      0      0      0      0      0      0      0      0
      0      0      0      0      0      0      0      0      0      0      0
      0      0      0      0      0      0      0      0      0      0      0
      0      0      0      0      0      0      0  14581  14581  14581      0
  14606  14606      0      0      0      0      0      0  14581  14581  14581
  14604  14606  14606      0      0      0      0      0      0      0      0
  14603  14606  14605  14596  14596      0      0      0      0      0      0
  14604  14604      0  14595  14596  14596      0      0      0      0      0
      0      0      0      0  14590  14590      0      0      0      0      0
      0      0  14598  14598  14597  14595  14594      0      0      0      0
      0      0      0  14598  14598  14587  14590      0      0      0      0
      0      0      0      0      0  14570  14570  14570      0      0      0
      0      0      0      0      0      0  14570  14570      0      0      0
      0      0      0      0      0      0      0      0      0      0      0
      0      0      0      0      0      0      0      0      0      0      0
  14627      0      0      0      0      0      0      0      0      0      0
      0      0      0      0      0      0      0      0      0      0      0
      0      0      0      0      0      0      0      0      0      0      0
      0      0      0      0      0      0      0      0      0      0      0
      0      0      0      0      0      0      0      0      0      0      0
      0      0      0      0      0      0      0      0      0      0      0
      0      0      0      0      0      0      0      0      0      0      0
  14620      0      0      0      0      0      0      0      0      0      0
  14619  14620      0      0      0      0  14598  14598  14598      0      0
  14612  14611  14612      0      0      0      0  14598  14598  14598      0
  14613  14606  14607  14609      0      0      0      0      0      0      0
      0      0  14609  14609  14608  14599      0      0      0      0      0
  14581  14598  14601      0      0  14599  14599      0      0      0      0
  14581  14588  14601  14601      0  14581  14584  14587      0      0      0
  14586  14581  14597  14601  14591  14589  14575  14575      0      0      0
  14598      0      0      0  14590  14589  14589      0      0      0      0
  14597  14595  14580      0      0  14594  14594  14594      0      0      0
  14587  14581  14582  14580  14581  14582  14594  14594      0      0      0
  14615  14609  14593  14587  14583  14586  14592  14594      0      0      0
  14630  14633  14629  14602  14610  14628  14605  14602      0      0      0
  14645  14649  14650  14645  14640  14634  14630  14616  14610  14606  14593
  14659  14663  14667  14677  14642  14645  14642  14634  14607  14606      0
  14662  14662      0  14702  14702  14687  14649  14641  14621  14621      0
      0      0      0      0  14689  14691  14667  14650  14627  14621  14621
  14646  14633  14640  14685  14681  14677  14679      0      0      0  14646
  14647  14635  14630  14660  14685  14667  14656  14634  14624  14624      0
  14602  14619  14619  14620  14643  14644  14640  14639  14623  14623      0
  14578  14567  14597  14601  14614  14643  14639  14636  14621  14619  14623
  14574  14568  14571  14583  14590      0      0      0  14592  14592  14593
  14575  14566  14564  14572  14576      0      0      0      0  14592  14592
  14576  14571  14560  14558      0      0      0      0      0      0      0
      0      0  14561  14560      0      0      0      0      0      0      0

  Columns 12 through 22
3 upvote
  flag
Why not just use indexing/slicing to get those slices : val(:,:,:,:,1), val(:,:,:,:,2) ...val(:,:,:,:,10)? – Divakar
upvote
  flag
Based in your other question, are you splitting to save? If so, why not save in another format? – Guto
upvote
  flag
@Guto, yes i am splitting to save different files. I have tried to save in another format as discussed in my another question. – Vaishali Jain
upvote
  flag
@Divakar How to index them using a loop, As i want to save each val(:,:,:,:,1), val(:,:,:,:,2)...I am using this loop but getting only one variable in output for i=1:10 M_i=lst10(:,:,:,:,i); end – Vaishali Jain
upvote
  flag
@VaishaliJain Index into output with iterator : M_i(i) =lst10(:,:,:,:,i);? – Divakar

1 Answers 11

up vote -1 down vote accepted

You can do this, it will create N matrices, with the naming scheme M_number, where number goes from 1 to N:

% Create dummy 5D matrix
val = zeros(57,92,1,1,10);

% Create N matrices
N = 10;
for i = 1:N
    eval(sprintf('M_%d = val(:,:,1,1,%d);', i, i));
end
1 upvote
  flag
Thank you so much, this code has served my purpose perfectly. – Vaishali Jain
upvote
  flag
Now, i Have 10 variables in my workspace as M_1, M_2....., If i want to perform one function on all variables. How can i define these variables? – Vaishali Jain
upvote
  flag
2 upvote
  flag
Please don't teach bad programming practices. This destroys everything – Sardar Usama
upvote
  flag
@sardarUsama, can you elaborate? Someone asked a question, I answered it – xrr
upvote
  flag
Read these: #1, #2 , #3 – Sardar Usama
upvote
  flag
@SardarUsama Can you suggest some useful solution to the problem? – Vaishali Jain
upvote
  flag
@VaishaliJain The best solution is what Divakar suggested in the comments. I suggest the same – Sardar Usama
upvote
  flag
How to do that ? – Vaishali Jain
upvote
  flag
@VaishaliJain Exactly how he suggested. i.e use val(:,:,:,:,1) where you need to use M_1 and so on. – Sardar Usama
upvote
  flag
@SardarUsama As i want to save each val(:,:,:,:,1), val(:,:,:,:,2)...I am using this loop but getting only one variable in output for i=1:10 M_i=lst10(:,:,:,:,i); end – Vaishali Jain
upvote
  flag
@VaishaliJain This is totally opposite to Divakar's suggestion and this is exactly what you have asked in your other question and I have answered you not to do that with explanation why that doesn't work – Sardar Usama
upvote
  flag
That means, I have to define each variable as M_1=val(:,:,:,:,1) ; M_2=val(:,:,:,:,2) ...... so on ? – Vaishali Jain
upvote
  flag
No. It means : don't define them as separate variable. Use val(:,:,:,:,1) , val(:,:,:,:,2) etc directly – Sardar Usama
upvote
  flag
is there any way to generate/define variable names automatically in a series as M_1, M_2, M_3....? – Vaishali Jain
upvote
  flag
@VaishaliJain I am telling you in my every comment to never ever do that. – Sardar Usama

Not the answer you're looking for? Browse other questions tagged or ask your own question.