wrong dimensions on a 3D array

1 view (last 30 days)
Hi, I am new to MATLAB
I have been given a file with 72 images that are 51*51 that opens in matlab as a 72*51*51 3D matrix, however I can't seem to index out the individual images.
I would like to be able to isolate each of the images so I can multiply them element wise with another 51*51 filter matrix I made but the way I am doing it gives me an image that is the wrong size to multiply.
thanks

Accepted Answer

Walter Roberson
Walter Roberson on 29 Nov 2020
%the below needs R2020b or later
re_arranged = permute(Matrix, [2 3 1]);
filtered = pagetimes(re_arranged, filter_matrix);
The result will be 51 x 51 x 72.
For earlier releases, there is a File Exchange contribution to do page-wise multiplication. Or you can loop:
re_arranged = permute(Matrix, [2 3 1]);
filtered = zeros(size(re_arranged));
for P = 1 : size(re_arranged,3)
filtered(:,:,P) = pagetimes(re_arranged(:,:,P), filter_matrix);
end
The permute is not strictly necessary. You could skip it and use
filtered = zeros(size(Matrix));
n = size(Matrix,2);
for P = 1 : size(Matrix,1)
filtered(P, :, :) = reshape( reshape(Matrix(P,:,:), [n, n]) * filter_matrix, [1, n, n]);
end
The output would be 72 x 51 x 51
  1 Comment
cameron lord
cameron lord on 30 Nov 2020
thanks that's great, used the permute function

Sign in to comment.

More Answers (0)

Categories

Find more on Resizing and Reshaping 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!