MATLAB: Operation on arrray to change each of its element.

arrray

I have a 2*n array say X. I want another 2*2*n array where (:,:,i) = X(:,i)*transpose(X(:,i)). How to achieve it without using for loop.
I have tried using
i = 1:n;
then assigning
cov = (:,:,i) = X(:,i)*transpose(X(:,i));
but it is not working

Best Answer

If you are using R2020b, then MATLAB just released pagemtimes() function which does what you want
X = rand(2, 10);
X2 = reshape(X, 2, 1, []);
Y = pagemtimes(X2, 'none', X2, 'transpose');
on older releases use
Y = bsxfun(@mtimes, reshape(X, 2, 1, []), reshape(X, 1, 2, []))