MATLAB: How to operate on 2d matrices in a 3D array

MATLABmatrix manipulation

Hi,
I have a set of n 3×3 matricies orgenized in a 3D array with dimesions 3x3xn.
I want to operate on all 3×3 matricies: I want to get a vector of determinants and a vector of trace of each 3×3 matrix.
I also want to calculate a 3x3xn array which will hold the 3×3 inverse of each matix.
I can do it using a for loop, of couse, calculating in each iteration for the ith matrix.
Is there a simpler and faster way to do it?
Thanks,
Alon

Best Answer

data = randi([1,10], 3,3,4); % your matrix with n = 4
data_det = arrayfun(@(i)det(data(:,:,i)), 1:size(data,3));
data_trace = arrayfun(@(i)trace(data(:,:,i)), 1:size(data,3));
data_inv = arrayfun(@(i)inv(data(:,:,i)), 1:size(data,3), 'UniformOutput', false);