MATLAB: How to convert a matrix of vectors in a matrix of skew symmetric matrices

vectorisation

I have a mxnx3 dimensional matrix (for example, 1000X2000 points represented by their x,y,z coordinates). I want to convert the last 3 dimensional vector into a skew symmetric matrix. I know that I can convert a single vector of size 3 in a skew symmetric matrix of size 3×3 as follows:
X = [ 0 -x(3) x(2) ;
x(3) 0 -x(1) ;
-x(2) x(1) 0 ];
Now, I can go write a for loop and go through each of mxn elements and convert them by the aforementioned method into a skey symmetric matrix. I was wondering, how to write this code in vectorised manner, so that I can run this quickly.

Best Answer

I think this does what you want
% Set the random seed, for replicability
rng default
% Generate some pretend data
M = 2;
N = 4;
ij3 = rand(M,N,3);
% Initialize the skew array
ij33 = zeros(M,N,3,3);
% Populate the skew array
ij33(:,:,1,2) = -ij3(:,:,3);
ij33(:,:,2,1) = ij3(:,:,3);
ij33(:,:,1,3) = ij3(:,:,2);
ij33(:,:,3,1) = -ij3(:,:,2);
ij33(:,:,2,3) = -ij3(:,:,1);
ij33(:,:,3,2) = ij3(:,:,1);