Extracting Matrix/Matrices from a 4D Matrix

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

 Réponse acceptée

which_to_extract = [2941, 5891, 5907,5872, 5883];
just_those = BIG(:, :, which_to_extract, :);

4 commentaires

Hey Walter! From a scale of 0 to 10, you are an AWESOME 10 man!!!!!!! Thank you so much, I can't believe I was trying to use a loop for this! lol Do you have any recommendations on the most efficient method to extract on the 1st column from each (5*5) matrix (So that's like 30 columns total: or a matrix (5*30)); I seriously can NOT thank you enough, sir! Kind Regards, Amine
Walter Roberson
Walter Roberson le 18 Oct 2016
Modifié(e) : Walter Roberson le 18 Oct 2016
just_those_first_column = permute( BIG(1, :, which_to_extract, :), [2 3 4 1]);
This would not be 5 x 30, this would be 5 x length(which_to_extract) x 30 .
In the particular case of extracting one matrix, such as just 2941, then
just_ones_first_columns = squeeze( BIG(1, :, 2941, : ) );
That would be 5 x 30.
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
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by