MATLAB: Is there any way for two reshape to cancel each other and two permute to each other
<p>I have a 3 by 3 complex matrix <code>c</code> such that I have to do the following operations. <code>c</code> is in general complex, <code>c</code> can also be larger but it is a square matrix.</p>

<pre><code>   c= rand(3,3)   
    A = bsxfun(@mtimes,permute(reshape(reshape(permute(c(:,1:end~=2)',[2 1]),1,3*2).', 3, []),[3,1,2]),c(:,1))
    conj(permute(A,[2 1 3]))
</code></pre>

<p>However, times take too long for this. Which part is the most time-consuming?</p>

<p>First of all, <code>c(:,1:end~=2)'</code> is a complex conjugate transpose of the matrix without the 2nd columns.</p>

<pre><code>c = rand(3,3)
c =
    0.9791    0.2003    0.9764
    0.5933    0.2570    0.4920
    0.5811    0.6384    0.9031

c(:,1:end~=2)'
ans =
    0.9791    0.5933    0.5811
    0.9764    0.4920    0.9031
</code></pre>

<p>Second, <code>permute(c(:,1:end~=2)',[2 1])</code> is a trick to make the reshape works row-wise.</p>

<pre><code>permute(c(:,1:end~=2)',[2 1])
ans =
    0.9791    0.9764
    0.5933    0.4920
    0.5811    0.9031
</code></pre>

<p>Third, do the reshape and <code>.'</code> to make it a column:</p>

<pre><code>reshape(permute(c(:,1:end~=2)',[2 1]),1,3*2).'
ans =
    0.9791
    0.5933
    0.5811
    0.9764
    0.4920
    0.9031
</code></pre>

<p>Fourth, reshape the column to a 2 by 3 matrix:</p>

<pre><code>reshape(reshape(permute(c(:,1:end~=2)',[2 1]),1,3*2).', 3, [])
ans =
    0.9791    0.9764
    0.5933    0.4920
    0.5811    0.9031
</code></pre>

<p>Fifth, create a multidimensional array with each layer being a row vector of the column (just reordering, not complex conjugation is taken).</p>

<pre><code>permute(reshape(reshape(permute(c(:,1:end~=2)',[2 1]),1,3*2).', 3, []),[3,1,2])
ans(:,:,1) =
    0.9791    0.5933    0.5811
ans(:,:,2) =
    0.9764    0.4920    0.9031
</code></pre>

<p>Sixth, use bsxfun to get the outer product of first column of <code>c</code> with each layer</p>

<pre><code>bsxfun(@mtimes,permute(reshape(reshape(permute(c(:,1:end~=2)',[2 1]),1,3*2).', 3, []),[3,1,2]),c(:,1))
ans(:,:,1) =
    0.9587    0.5809    0.5689
    0.5809    0.3520    0.3448
    0.5689    0.3448    0.3376
ans(:,:,2) =
    0.9560    0.4817    0.8843
    0.5793    0.2919    0.5359
    0.5673    0.2859    0.5248
</code></pre>

<p>The last I don't think can be simplified. Its purpose is to take the complex conjugate of matrix at each layer:</p>

<pre><code>A = bsxfun(@mtimes,permute(reshape(reshape(permute(c(:,1:end~=2)',[2 1]),1,3*2).', 3, []),[3,1,2]),c(:,1))
A(:,:,1) =
    0.9587    0.5809    0.5689
    0.5809    0.3520    0.3448
    0.5689    0.3448    0.3376
A(:,:,2) =
    0.9560    0.4817    0.8843
    0.5793    0.2919    0.5359
    0.5673    0.2859    0.5248

conj(permute(A,[2 1 3]))
ans(:,:,1) =
    0.9587    0.5809    0.5689
    0.5809    0.3520    0.3448
    0.5689    0.3448    0.3376
ans(:,:,2) =
    0.9560    0.5793    0.5673
    0.4817    0.2919    0.2859
    0.8843    0.5359    0.5248
</code></pre>

<p><code>A(:,:,1)</code> is symmetric by coincidence. </p>