Extracting Matrix/Matrices from a 4D Matrix

19 views (last 30 days)
Hello Matlab Wizards, Hope everyone is doing well. I need some directions with the most efficient one to extract a matrix or numerous matrices ( 5 by 5 dimension each) from a 4-D Double matrix. This is what I have:
My final matrix, BIG, dimension is : 5 *5 * 14680 * 30 ( so basically 14680 of "5*5" matrices and 30 sets of those).
Suppose I want to extract Matrix A ;number 2941, then it is located in final matrix: A=BIG(:,:,2941,1:30);
is that correct?
Now what If I need to extract numerous matrices from Big, suppose matrix number 5891, 5907,5872, and 5883, so how do I do it taking in consideration that each (5*5) matrix has to be picked 30 times (1:30)?
I did this :
for i=1:30;
Matrix_A(:,:,2941,i)=BIG(:,:,2941,i);
end
I got some strange results ( like many empty 5*5).
Do you think I can stack the extracted matrices horizontally or vertically some how, because I will eventually need to get a specific vector from each (1:5,1) [a "5*1" vector from each extracted matrix).
Please let me know if anyone has a suggestion, and I will try to explain better if not clear. I greatly apprciate the help in advance! Kind Regards

Accepted Answer

Walter Roberson
Walter Roberson on 17 Oct 2016
which_to_extract = [2941, 5891, 5907,5872, 5883];
just_those = BIG(:, :, which_to_extract, :);
  4 Comments
Amine Ben Ayara
Amine Ben Ayara on 18 Oct 2016
Hello Mr. Roberson, I am sorry for not being clear enough. I am actually still trying to grasp how the matrices are constructed into the 4D to figure out how I need them reshaped into 2D. So basically, I have five (5*5) matrices ([2941, 5891, 5907,5872, 5883]) and also I have 30 sets of those, hence (5 by 5 by 5, by 30). I can either reshape the 4D so the final matrix will be: 25*150 ( basically, in my own way of understanding, horizontally stacking all the 30 sets of 5by5 matrices to get 25 rows by 150 column. From there and once it is in 2D format I can just try to manually extract elements I need( My inefficient way of working in Matlab :( ). Or basically, try to extract only the very first column from every set of (5by5) matrix and stack those columns horizontally to get a matrix of 25by30. Either was possible is fine, I tried loops, permute and reshape and still getting strange results. Thank you so much for taking the time to advise me. Kind Regards Amine
Walter Roberson
Walter Roberson on 18 Oct 2016
reshape( BIG(:, 1, which_to_extract, :), [], size(BIG,4) )
or possibly
reshape( permute( BIG(:, 1, which_to_extract, :), [3 1 4 2]), [], size(BIG,4) )
the difference is the stacking order, whether it is to go "across" or "down" before you make the result into a column. You just happen to be working with size(BIG,2) equal to length(which_to_extract) so I have not been able to figure out which order you want. If you had 4 to extract instead of 5, would you want to make it 5 x 4 x 30 and then reshape that to (5*4) x 30 ? Or would you want it to be 4 x 5 x 30 and then reshape to (4*5) x 30 ? The number of elements is the same in each case but the order is not the same.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!