<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172652</link>
    <title>MATLAB Central Newsreader - vectorise a reshape loop</title>
    <description>Feed for thread: vectorise a reshape loop</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Wed, 16 Jul 2008 13:04:02 -0400</pubDate>
      <title>vectorise a reshape loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172652#443380</link>
      <author>Dave Brackett</author>
      <description>Hi, I am having trouble vectorising the below for loop. Can&lt;br&gt;
anyone help me please? I am expecting M to consist of 3 rows&lt;br&gt;
of 2x2 matrices each with 16 deep. Thanks in advance.&lt;br&gt;
&lt;br&gt;
for p=1:3&lt;br&gt;
q=p;&lt;br&gt;
M=reshape([a(:,q) c(:,q) b(:,q) d(:,q)].', 2, 2, []);&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
where a,b,c,d are 3x16 matrice such as: &lt;br&gt;
(imaginary parts can be ignored)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;1.0000 + 0.0000i   0.9999 + 0.0000i   0.9999 + 0.0001i&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;1.0000 + 0.0000i   0.9999 + 0.0000i   0.9999 + 0.0001i&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;1.0000 + 0.0000i   0.9999 + 0.0000i   0.9999 + 0.0001i&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;1.0000 + 0.0000i   0.9999 + 0.0000i   0.9999 + 0.0001i&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;1.0004 + 0.0000i   1.0003 + 0.0000i   1.0002 + 0.0001i&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;1.0004 + 0.0000i   1.0003 + 0.0000i   1.0002 + 0.0001i&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;1.0003 + 0.0000i   1.0003 + 0.0000i   1.0002 + 0.0001i&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;1.0003 + 0.0000i   1.0003 + 0.0000i   1.0002 + 0.0001i&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;1.0051 + 0.0000i   1.0050 + 0.0000i   1.0050 + 0.0001i&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;1.0068 + 0.0000i   1.0068 + 0.0000i   1.0067 + 0.0001i&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;1.0111 + 0.0000i   1.0110 + 0.0000i   1.0110 + 0.0001i&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;1.0178 + 0.0000i   1.0178 + 0.0000i   1.0177 + 0.0001i&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;1.0500 + 0.0000i   1.0499 + 0.0000i   1.0498 + 0.0000i&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;1.1666 + 0.0000i   1.1666 + 0.0000i   1.1665 + 0.0000i&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;1.2666 + 0.0000i   1.2666 + 0.0000i   1.2665 + 0.0000i&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;3.2499 + 0.0000i   3.2497 + 0.0000i   3.2493 + 0.0000i</description>
    </item>
    <item>
      <pubDate>Wed, 16 Jul 2008 13:37:02 -0400</pubDate>
      <title>Re: vectorise a reshape loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172652#443388</link>
      <author>John D'Errico</author>
      <description>&quot;Dave Brackett&quot; &amp;lt;davebrackett@hotmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;lt;g5krk2$77v$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi, I am having trouble vectorising the below for loop. Can&lt;br&gt;
&amp;gt; anyone help me please? I am expecting M to consist of 3 rows&lt;br&gt;
&amp;gt; of 2x2 matrices each with 16 deep. Thanks in advance.&lt;br&gt;
&lt;br&gt;
Try that again? What is the shape of M?&lt;br&gt;
Is it 3x2x2x16? This is inconsistent with&lt;br&gt;
what you have shown below.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&amp;gt; for p=1:3&lt;br&gt;
&amp;gt; q=p;&lt;br&gt;
&amp;gt; M=reshape([a(:,q) c(:,q) b(:,q) d(:,q)].', 2, 2, []);&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; where a,b,c,d are 3x16 matrice such as: &lt;br&gt;
&amp;gt; (imaginary parts can be ignored)&lt;br&gt;
&amp;gt;    1.0000 + 0.0000i   0.9999 + 0.0000i   0.9999 + 0.0001i&lt;br&gt;
&lt;br&gt;
Um, that is a 16x3 matrix.&lt;br&gt;
&lt;br&gt;
Anyway, what loop?&lt;br&gt;
&lt;br&gt;
Since you overwrite M on each pass&lt;br&gt;
through, that non-loop does nothing&lt;br&gt;
but define M at the last value of p.&lt;br&gt;
&lt;br&gt;
We can't help you unless you will think&lt;br&gt;
clearly enough about what you want to&lt;br&gt;
explain it.&lt;br&gt;
&lt;br&gt;
You might start by preallocating M to be&lt;br&gt;
the proper size. Use zeros.&lt;br&gt;
&lt;br&gt;
John</description>
    </item>
    <item>
      <pubDate>Wed, 16 Jul 2008 15:03:02 -0400</pubDate>
      <title>Re: vectorise a reshape loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172652#443403</link>
      <author>Dave Brackett</author>
      <description>&quot;John D'Errico&quot; &amp;lt;woodchips@rochester.rr.com&amp;gt; wrote in&lt;br&gt;
message &amp;lt;g5kthu$svb$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Dave Brackett&quot; &amp;lt;davebrackett@hotmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;gt; &amp;lt;g5krk2$77v$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Hi, I am having trouble vectorising the below for loop. Can&lt;br&gt;
&amp;gt; &amp;gt; anyone help me please? I am expecting M to consist of 3 rows&lt;br&gt;
&amp;gt; &amp;gt; of 2x2 matrices each with 16 deep. Thanks in advance.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Try that again? What is the shape of M?&lt;br&gt;
&amp;gt; Is it 3x2x2x16? This is inconsistent with&lt;br&gt;
&amp;gt; what you have shown below.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; for p=1:3&lt;br&gt;
&amp;gt; &amp;gt; q=p;&lt;br&gt;
&amp;gt; &amp;gt; M=reshape([a(:,q) c(:,q) b(:,q) d(:,q)].', 2, 2, []);&lt;br&gt;
&amp;gt; &amp;gt; end&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; where a,b,c,d are 3x16 matrice such as: &lt;br&gt;
&amp;gt; &amp;gt; (imaginary parts can be ignored)&lt;br&gt;
&amp;gt; &amp;gt;    1.0000 + 0.0000i   0.9999 + 0.0000i   0.9999 + 0.0001i&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Um, that is a 16x3 matrix.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Anyway, what loop?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Since you overwrite M on each pass&lt;br&gt;
&amp;gt; through, that non-loop does nothing&lt;br&gt;
&amp;gt; but define M at the last value of p.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; We can't help you unless you will think&lt;br&gt;
&amp;gt; clearly enough about what you want to&lt;br&gt;
&amp;gt; explain it.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; You might start by preallocating M to be&lt;br&gt;
&amp;gt; the proper size. Use zeros.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; John&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
Ok, sorry for any confusion, I will try again.&lt;br&gt;
&lt;br&gt;
I want a 3D matrix, consisting of in this case, 3 2x2&lt;br&gt;
matrices (as shown below) plus 15 additional 2x2 matrices&lt;br&gt;
stacked behind them, giving 3 rows of 16 2x2 matrices all&lt;br&gt;
contained within one matrix.&lt;br&gt;
&lt;br&gt;
[a1 b1  [a2 b2  [a3 b3&lt;br&gt;
&amp;nbsp;c1 d1]  c2 d2]  c3 d3]&lt;br&gt;
&lt;br&gt;
The amended 'for' loop shown below intends to do this. I&lt;br&gt;
have changed it so M should store each 2x2 matrix, but it is&lt;br&gt;
isn't right as I'm not sure how to do it right.&lt;br&gt;
&lt;br&gt;
for p=1:3&lt;br&gt;
q=p;&lt;br&gt;
r=1:16;&lt;br&gt;
M(:,q,r)=reshape([a(:,q) c(:,q) b(:,q) d(:,q)].', 2, 2, []);&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
As you rightly point out a,b,c, and d are indeed 16x3&lt;br&gt;
matrices. Preallocation of M can come later.&lt;br&gt;
&lt;br&gt;
Hopefully that explanation is a bit clearer. Thanks.</description>
    </item>
    <item>
      <pubDate>Thu, 17 Jul 2008 09:50:05 -0400</pubDate>
      <title>Re: vectorise a reshape loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172652#443574</link>
      <author>Dave Brackett</author>
      <description>&quot;John D'Errico&quot; &amp;lt;woodchips@rochester.rr.com&amp;gt; wrote in&lt;br&gt;
message &amp;lt;g5kthu$svb$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Dave Brackett&quot; &amp;lt;davebrackett@hotmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;gt; &amp;lt;g5krk2$77v$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Hi, I am having trouble vectorising the below for loop. Can&lt;br&gt;
&amp;gt; &amp;gt; anyone help me please? I am expecting M to consist of 3 rows&lt;br&gt;
&amp;gt; &amp;gt; of 2x2 matrices each with 16 deep. Thanks in advance.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Try that again? What is the shape of M?&lt;br&gt;
&amp;gt; Is it 3x2x2x16? This is inconsistent with&lt;br&gt;
&amp;gt; what you have shown below.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; for p=1:3&lt;br&gt;
&amp;gt; &amp;gt; q=p;&lt;br&gt;
&amp;gt; &amp;gt; M=reshape([a(:,q) c(:,q) b(:,q) d(:,q)].', 2, 2, []);&lt;br&gt;
&amp;gt; &amp;gt; end&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; where a,b,c,d are 3x16 matrice such as: &lt;br&gt;
&amp;gt; &amp;gt; (imaginary parts can be ignored)&lt;br&gt;
&amp;gt; &amp;gt;    1.0000 + 0.0000i   0.9999 + 0.0000i   0.9999 + 0.0001i&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Um, that is a 16x3 matrix.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Anyway, what loop?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Since you overwrite M on each pass&lt;br&gt;
&amp;gt; through, that non-loop does nothing&lt;br&gt;
&amp;gt; but define M at the last value of p.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; We can't help you unless you will think&lt;br&gt;
&amp;gt; clearly enough about what you want to&lt;br&gt;
&amp;gt; explain it.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; You might start by preallocating M to be&lt;br&gt;
&amp;gt; the proper size. Use zeros.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; John&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
Ok, sorry for any confusion, I will try again.&lt;br&gt;
&lt;br&gt;
I want a 3D matrix, consisting of in this case, 3 2x2&lt;br&gt;
matrices (as shown below) plus 15 additional 2x2 matrices&lt;br&gt;
stacked behind them, giving 3 rows of 16 2x2 matrices all&lt;br&gt;
contained within one matrix.&lt;br&gt;
&lt;br&gt;
[a1 b1  [a2 b2  [a3 b3&lt;br&gt;
&amp;nbsp;c1 d1]  c2 d2]  c3 d3]&lt;br&gt;
&lt;br&gt;
The amended 'for' loop shown below intends to do this. I&lt;br&gt;
have changed it so M should store each 2x2 matrix, but it is&lt;br&gt;
isn't right as I'm not sure how to do it right.&lt;br&gt;
&lt;br&gt;
for p=1:3&lt;br&gt;
q=p;&lt;br&gt;
r=1:16;&lt;br&gt;
M(:,q,r)=reshape([a(:,q) c(:,q) b(:,q) d(:,q)].', 2, 2, []);&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
As you rightly point out a,b,c, and d are indeed 16x3&lt;br&gt;
matrices. Preallocation of M can come later.&lt;br&gt;
&lt;br&gt;
Hopefully that explanation is a bit clearer. Thanks.</description>
    </item>
    <item>
      <pubDate>Thu, 17 Jul 2008 11:08:02 -0400</pubDate>
      <title>Re: vectorise a reshape loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172652#443589</link>
      <author>Dave Brackett</author>
      <description>cut out quotes from previous posts as they are making the&lt;br&gt;
post unreadable, also sorry for double post of previous&lt;br&gt;
reply - don't know what happened there!&lt;br&gt;
&lt;br&gt;
ok, i have spent some more time and the below for loop now&lt;br&gt;
shows what I am trying to achieve. However, the cells are&lt;br&gt;
causing me some trouble as i don't get 2x2 matrices for each&lt;br&gt;
prodH entry. I have read that cells don't allow&lt;br&gt;
vectorisation so is there a better way that someone can&lt;br&gt;
suggest? Thanks.&lt;br&gt;
&lt;br&gt;
M=cell(1,3,16);&lt;br&gt;
prodH=cell(1,3);&lt;br&gt;
&lt;br&gt;
for p=1:3&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;q=p;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for r=1:16;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;M(1,q,r)={reshape([ai_col(r,q) ci_col(r,q)&lt;br&gt;
bi_col(r,q) di_col(r,q)].', 2, 2, [])}&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;prodH(1,q)={[ndfun('mprod', M(1,q,:))]} &lt;br&gt;
end</description>
    </item>
    <item>
      <pubDate>Thu, 17 Jul 2008 11:13:02 -0400</pubDate>
      <title>Re: vectorise a reshape loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172652#443591</link>
      <author>John D'Errico</author>
      <description>&quot;Dave Brackett&quot; &amp;lt;davebrackett@hotmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;lt;g5n4kd$jtp$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;John D'Errico&quot; &amp;lt;woodchips@rochester.rr.com&amp;gt; wrote in&lt;br&gt;
&amp;gt; message &amp;lt;g5kthu$svb$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; &quot;Dave Brackett&quot; &amp;lt;davebrackett@hotmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;gt; &amp;gt; &amp;lt;g5krk2$77v$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Hi, I am having trouble vectorising the below for loop. Can&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; anyone help me please? I am expecting M to consist of 3 rows&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; of 2x2 matrices each with 16 deep. Thanks in advance.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Try that again? What is the shape of M?&lt;br&gt;
&amp;gt; &amp;gt; Is it 3x2x2x16? This is inconsistent with&lt;br&gt;
&amp;gt; &amp;gt; what you have shown below.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; for p=1:3&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; q=p;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; M=reshape([a(:,q) c(:,q) b(:,q) d(:,q)].', 2, 2, []);&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; end&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; where a,b,c,d are 3x16 matrice such as: &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; (imaginary parts can be ignored)&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt;    1.0000 + 0.0000i   0.9999 + 0.0000i   0.9999 + 0.0001i&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Um, that is a 16x3 matrix.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Anyway, what loop?&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Since you overwrite M on each pass&lt;br&gt;
&amp;gt; &amp;gt; through, that non-loop does nothing&lt;br&gt;
&amp;gt; &amp;gt; but define M at the last value of p.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; We can't help you unless you will think&lt;br&gt;
&amp;gt; &amp;gt; clearly enough about what you want to&lt;br&gt;
&amp;gt; &amp;gt; explain it.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; You might start by preallocating M to be&lt;br&gt;
&amp;gt; &amp;gt; the proper size. Use zeros.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; John&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Ok, sorry for any confusion, I will try again.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I want a 3D matrix, consisting of in this case, 3 2x2&lt;br&gt;
&amp;gt; matrices (as shown below) plus 15 additional 2x2 matrices&lt;br&gt;
&amp;gt; stacked behind them, giving 3 rows of 16 2x2 matrices all&lt;br&gt;
&amp;gt; contained within one matrix.&lt;br&gt;
&lt;br&gt;
Do you want a 4-d matrix, that is 2x2x3x16?&lt;br&gt;
&lt;br&gt;
Or does this totally unclear explanation&lt;br&gt;
request a 2x2x48 matrix? What do you&lt;br&gt;
mean when you say 3 rows 2x2 matrices?&lt;br&gt;
Does this ask for a 3x2x2 array? Or for a&lt;br&gt;
2x2x3 array?&lt;br&gt;
&lt;br&gt;
For some reason, you do not accept what&lt;br&gt;
it is you are trying to do, nor have you&lt;br&gt;
taken the time or effort to explain it clearly.&lt;br&gt;
Think clearly. If you want a single matrix&lt;br&gt;
that contains 3 sets of 2x2 matrices, 16&lt;br&gt;
times, then you have a 3x2x2x16 matrix.&lt;br&gt;
A 4-d matrix.&lt;br&gt;
&lt;br&gt;
Is this what you want?&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&amp;gt; [a1 b1  [a2 b2  [a3 b3&lt;br&gt;
&amp;gt;  c1 d1]  c2 d2]  c3 d3]&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The amended 'for' loop shown below intends to do this. I&lt;br&gt;
&amp;gt; have changed it so M should store each 2x2 matrix, but it is&lt;br&gt;
&amp;gt; isn't right as I'm not sure how to do it right.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for p=1:3&lt;br&gt;
&amp;gt; q=p;&lt;br&gt;
&amp;gt; r=1:16;&lt;br&gt;
&amp;gt; M(:,q,r)=reshape([a(:,q) c(:,q) b(:,q) d(:,q)].', 2, 2, []);&lt;br&gt;
&amp;gt; end&lt;br&gt;
&lt;br&gt;
Again, this loop merely (tries to) overwrite&lt;br&gt;
M on each pass through. Is this what you&lt;br&gt;
want? I don't think so from your description.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&lt;br&gt;
&amp;gt; As you rightly point out a,b,c, and d are indeed 16x3&lt;br&gt;
&amp;gt; matrices. Preallocation of M can come later.&lt;br&gt;
&lt;br&gt;
No. Preallocation should come FIRST, for&lt;br&gt;
several good reasons. Not the least of&lt;br&gt;
which is your acceptance of what you are&lt;br&gt;
trying to do.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&lt;br&gt;
&amp;gt; Hopefully that explanation is a bit clearer. Thanks.&lt;br&gt;
&lt;br&gt;
Clear now. As mud.&lt;br&gt;
&lt;br&gt;
What is your goal? If it is as I have suggested&lt;br&gt;
it might be, then you can do everything in&lt;br&gt;
one line. I'll do it in two lines to make it&lt;br&gt;
more understandable.&lt;br&gt;
&lt;br&gt;
M = reshape([a(:),b(:),c(:),d(:)],16,3,2,2);&lt;br&gt;
M = permute(M,[3 4 2 1]);&lt;br&gt;
&lt;br&gt;
This will create a single 2x2x3x16 array.&lt;br&gt;
&lt;br&gt;
So far, I still have no idea if this is what&lt;br&gt;
you really want. So slow down. Think&lt;br&gt;
clearly about what you are doing. Then&lt;br&gt;
explain it clearly. Communication is a&lt;br&gt;
valuable skill. Learn it.&lt;br&gt;
&lt;br&gt;
John</description>
    </item>
    <item>
      <pubDate>Thu, 17 Jul 2008 13:12:02 -0400</pubDate>
      <title>Re: vectorise a reshape loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172652#443617</link>
      <author>Dave Brackett</author>
      <description>I think the communication issue stems from the fact i didn't&lt;br&gt;
understand the number of array dimensions could be above 3.&lt;br&gt;
To me, a matrix with rows, columns, and pages was always a&lt;br&gt;
3D matrix (as it forms a 3D 'block'). Evidently this is not&lt;br&gt;
the case.&lt;br&gt;
&lt;br&gt;
The reshape and permute functions you suggest do achieve&lt;br&gt;
what I wanted thanks:&lt;br&gt;
M=reshape([ai_col(:),bi_col(:),ci_col(:),di_col(:)],16,3,2,2);&lt;br&gt;
M=permute(M,[3 4 2 1]);&lt;br&gt;
&lt;br&gt;
For each of the 3 sets of 16 2x2 arrays contained within M I&lt;br&gt;
want to find the product in the dimension of the 16 2x2&lt;br&gt;
matrices. I have been using your ndfun mex file as follows:&lt;br&gt;
&lt;br&gt;
prodH=zeros(2,2,3);&lt;br&gt;
for p=1:3&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;q=p;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;prodH(1,q)=ndfun('mprod',M(:,:,p,:))&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
However, i get the error that 'assignment has more&lt;br&gt;
non-singleton rhs dimensions than non-singleton subscripts'.&lt;br&gt;
Could you show me how to do this correctly pls? Thanks for&lt;br&gt;
your help.</description>
    </item>
    <item>
      <pubDate>Thu, 17 Jul 2008 13:59:03 -0400</pubDate>
      <title>Re: vectorise a reshape loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172652#443627</link>
      <author>John D'Errico</author>
      <description>&quot;Dave Brackett&quot; &amp;lt;davebrackett@hotmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;lt;g5ngf2$fdp$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I think the communication issue stems from the fact i didn't&lt;br&gt;
&amp;gt; understand the number of array dimensions could be above 3.&lt;br&gt;
&amp;gt; To me, a matrix with rows, columns, and pages was always a&lt;br&gt;
&amp;gt; 3D matrix (as it forms a 3D 'block'). Evidently this is not&lt;br&gt;
&amp;gt; the case.&lt;br&gt;
&lt;br&gt;
Ah. This was the problem. Yes, I don't think&lt;br&gt;
there is any real limit on the number of&lt;br&gt;
dimensions. Yes, there may be a limit, but&lt;br&gt;
it is probably some large number.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&lt;br&gt;
&amp;gt; The reshape and permute functions you suggest do achieve&lt;br&gt;
&amp;gt; what I wanted thanks:&lt;br&gt;
&amp;gt; M=reshape([ai_col(:),bi_col(:),ci_col(:),di_col(:)],16,3,2,2);&lt;br&gt;
&amp;gt; M=permute(M,[3 4 2 1]);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; For each of the 3 sets of 16 2x2 arrays contained within M I&lt;br&gt;
&amp;gt; want to find the product in the dimension of the 16 2x2&lt;br&gt;
&amp;gt; matrices. I have been using your ndfun mex file as follows:&lt;br&gt;
&lt;br&gt;
Its not my ndfun.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&amp;gt; prodH=zeros(2,2,3);&lt;br&gt;
&amp;gt; for p=1:3&lt;br&gt;
&amp;gt;     q=p;&lt;br&gt;
&amp;gt;     prodH(1,q)=ndfun('mprod',M(:,:,p,:))&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; However, i get the error that 'assignment has more&lt;br&gt;
&amp;gt; non-singleton rhs dimensions than non-singleton subscripts'.&lt;br&gt;
&amp;gt; Could you show me how to do this correctly pls? Thanks for&lt;br&gt;
&amp;gt; your help.&lt;br&gt;
&lt;br&gt;
What would be the result of this product?&lt;br&gt;
Perhaps just a 2x2 array? How do you&lt;br&gt;
expect a 2x2 array to fit into a single&lt;br&gt;
array element of prodH? Why would you&lt;br&gt;
index the result as prodH(1,q) ?&lt;br&gt;
&lt;br&gt;
If that result is a 2x2 array, and you have&lt;br&gt;
preallocated prodH as 2x2x3, then should&lt;br&gt;
you not do it as&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;prodH(:,:,q)=ndfun('mprod',M(:,:,p,:));&lt;br&gt;
&lt;br&gt;
John</description>
    </item>
    <item>
      <pubDate>Thu, 17 Jul 2008 14:12:06 -0400</pubDate>
      <title>Re: vectorise a reshape loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172652#443632</link>
      <author>Dave Brackett</author>
      <description>&amp;gt; Its not my ndfun.&lt;br&gt;
&lt;br&gt;
sorry, not sure why i suggested it was yours. it is from&lt;br&gt;
&lt;a href=&quot;http://www.mit.edu/~pwb/matlab/ndfun/ndfun.m&quot;&gt;http://www.mit.edu/~pwb/matlab/ndfun/ndfun.m&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&lt;br&gt;
&amp;gt; What would be the result of this product?&lt;br&gt;
&amp;gt; Perhaps just a 2x2 array? How do you&lt;br&gt;
&amp;gt; expect a 2x2 array to fit into a single&lt;br&gt;
&amp;gt; array element of prodH? Why would you&lt;br&gt;
&amp;gt; index the result as prodH(1,q) ?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; If that result is a 2x2 array, and you have&lt;br&gt;
&amp;gt; preallocated prodH as 2x2x3, then should&lt;br&gt;
&amp;gt; you not do it as&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;    prodH(:,:,q)=ndfun('mprod',M(:,:,p,:));&lt;br&gt;
&lt;br&gt;
after i had posted that i realised it was wrong and my line&lt;br&gt;
in the for loop now looks like:&lt;br&gt;
prodH(:,:,q)=ndfun('mprod',M(:,:,q,:))&lt;br&gt;
&lt;br&gt;
however, i still get the same error. i am expecting 3 2x2&lt;br&gt;
arrays, 1 for each product of the 16 arrays.</description>
    </item>
    <item>
      <pubDate>Thu, 17 Jul 2008 15:38:42 -0400</pubDate>
      <title>Re: vectorise a reshape loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172652#443657</link>
      <author>Peter Boettcher</author>
      <description>&quot;Dave Brackett&quot; &amp;lt;davebrackett@hotmail.com&amp;gt; writes:&lt;br&gt;
&lt;br&gt;
&amp;gt; I think the communication issue stems from the fact i didn't&lt;br&gt;
&amp;gt; understand the number of array dimensions could be above 3.&lt;br&gt;
&amp;gt; To me, a matrix with rows, columns, and pages was always a&lt;br&gt;
&amp;gt; 3D matrix (as it forms a 3D 'block'). Evidently this is not&lt;br&gt;
&amp;gt; the case.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; The reshape and permute functions you suggest do achieve&lt;br&gt;
&amp;gt; what I wanted thanks:&lt;br&gt;
&amp;gt; M=reshape([ai_col(:),bi_col(:),ci_col(:),di_col(:)],16,3,2,2);&lt;br&gt;
&amp;gt; M=permute(M,[3 4 2 1]);&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; For each of the 3 sets of 16 2x2 arrays contained within M I&lt;br&gt;
&amp;gt; want to find the product in the dimension of the 16 2x2&lt;br&gt;
&amp;gt; matrices. I have been using your ndfun mex file as follows:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; prodH=zeros(2,2,3);&lt;br&gt;
&amp;gt; for p=1:3&lt;br&gt;
&amp;gt;     q=p;&lt;br&gt;
&amp;gt;     prodH(1,q)=ndfun('mprod',M(:,:,p,:))&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; However, i get the error that 'assignment has more&lt;br&gt;
&amp;gt; non-singleton rhs dimensions than non-singleton subscripts'.&lt;br&gt;
&amp;gt; Could you show me how to do this correctly pls? Thanks for&lt;br&gt;
&amp;gt; your help.&lt;br&gt;
&lt;br&gt;
prodH is 2x2x3.  You index it with only two indices.  What do you want&lt;br&gt;
the third index to be?&lt;br&gt;
&lt;br&gt;
For each thing you are trying to assign (in your case, only one!),&lt;br&gt;
figure out the dimensions of the right-hand side.  Try it first&lt;br&gt;
mentally, and if you're not sure, assign it to a variable and ask MATLAB&lt;br&gt;
to tell you what the size is.&lt;br&gt;
&lt;br&gt;
Then, look at where you want to assign it.  Your assignment indices&lt;br&gt;
must provide a place in the output which exactly matches the dimensions&lt;br&gt;
of the right side.&lt;br&gt;
&lt;br&gt;
For instance, I can't say:&lt;br&gt;
&lt;br&gt;
A(1, 2:4) = B(1:4, 2);&lt;br&gt;
&lt;br&gt;
Why?&lt;br&gt;
left side is 1x3, right side is 4x1&lt;br&gt;
&lt;br&gt;
I can't say:&lt;br&gt;
A(1,1,2:4) = B(1,2:4)&lt;br&gt;
&lt;br&gt;
Why?&lt;br&gt;
left side is 1x1x3, right side is 1x3&lt;br&gt;
&lt;br&gt;
-Peter</description>
    </item>
    <item>
      <pubDate>Thu, 17 Jul 2008 16:36:32 -0400</pubDate>
      <title>Re: vectorise a reshape loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172652#443671</link>
      <author>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)</author>
      <description>In article &amp;lt;g5nj77$j6j$1@fred.mathworks.com&amp;gt;,&lt;br&gt;
John D'Errico &amp;lt;woodchips@rochester.rr.com&amp;gt; wrote:&lt;br&gt;
&amp;gt;&quot;Dave Brackett&quot; &amp;lt;davebrackett@hotmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;gt;&amp;lt;g5ngf2$fdp$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt;&amp;gt; I think the communication issue stems from the fact i didn't&lt;br&gt;
&amp;gt;&amp;gt; understand the number of array dimensions could be above 3.&lt;br&gt;
&amp;gt;&amp;gt; To me, a matrix with rows, columns, and pages was always a&lt;br&gt;
&amp;gt;&amp;gt; 3D matrix (as it forms a 3D 'block'). Evidently this is not&lt;br&gt;
&amp;gt;&amp;gt; the case.&lt;br&gt;
&lt;br&gt;
&amp;gt;Ah. This was the problem. Yes, I don't think&lt;br&gt;
&amp;gt;there is any real limit on the number of&lt;br&gt;
&amp;gt;dimensions. Yes, there may be a limit, but&lt;br&gt;
&amp;gt;it is probably some large number.&lt;br&gt;
&lt;br&gt;
The number of dimensions is -at least- 2^16+1&lt;br&gt;
&lt;br&gt;
foo = [5;7];&lt;br&gt;
bar = permute(foo,[2:65537,1]);&lt;br&gt;
&lt;br&gt;
I have it working on 2^20+1 but it is taking a fair bit of time.&lt;br&gt;
-- &lt;br&gt;
&amp;nbsp;&amp;nbsp;&quot;The slogans of an inadequate criticism peddle ideas to fashion&quot;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;-- Walter Benjamin</description>
    </item>
    <item>
      <pubDate>Mon, 21 Jul 2008 09:17:03 -0400</pubDate>
      <title>Re: vectorise a reshape loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172652#444276</link>
      <author>Dave Brackett</author>
      <description>&amp;gt; figure out the dimensions of the right-hand side.  Try it&lt;br&gt;
first&lt;br&gt;
&amp;gt; mentally, and if you're not sure, assign it to a variable&lt;br&gt;
and ask MATLAB&lt;br&gt;
&amp;gt; to tell you what the size is.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Then, look at where you want to assign it.  Your&lt;br&gt;
assignment indices&lt;br&gt;
&amp;gt; must provide a place in the output which exactly matches&lt;br&gt;
the dimensions&lt;br&gt;
&amp;gt; of the right side.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; For instance, I can't say:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; A(1, 2:4) = B(1:4, 2);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Why?&lt;br&gt;
&amp;gt; left side is 1x3, right side is 4x1&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I can't say:&lt;br&gt;
&amp;gt; A(1,1,2:4) = B(1,2:4)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Why?&lt;br&gt;
&amp;gt; left side is 1x1x3, right side is 1x3&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; -Peter&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
ok, the size of my right side is 2x2x90x16 and the required&lt;br&gt;
size for my left is 2x2x90x1 so I have written this as:&lt;br&gt;
&lt;br&gt;
prodH=zeros(2,2,length(freq),1);&lt;br&gt;
for p=1:length(freq)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;q=p;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;prodH(:,:,q,1)=ndfun('mprod',M(:,:,q,:))&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
I still get:&lt;br&gt;
&lt;br&gt;
??? Assignment has more non-singleton rhs dimensions than&lt;br&gt;
non-singleton&lt;br&gt;
subscripts&lt;br&gt;
&lt;br&gt;
Error in ==&amp;gt; new_for_gatool_2 at 110&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;prodH(:,:,q,1)=ndfun('mprod',M(:,:,q,:))&lt;br&gt;
&lt;br&gt;
What am i still doing wrong? Thanks for your help.</description>
    </item>
    <item>
      <pubDate>Mon, 21 Jul 2008 13:36:06 -0400</pubDate>
      <title>Re: vectorise a reshape loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172652#444310</link>
      <author>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)</author>
      <description>In article &amp;lt;g61k6f$1gj$1@fred.mathworks.com&amp;gt;,&lt;br&gt;
Dave Brackett &amp;lt;davebrackett@hotmail.com&amp;gt; wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt;    prodH(:,:,q,1)=ndfun('mprod',M(:,:,q,:))&lt;br&gt;
&lt;br&gt;
&amp;gt;I still get:&lt;br&gt;
&lt;br&gt;
&amp;gt;??? Assignment has more non-singleton rhs dimensions than&lt;br&gt;
&amp;gt;non-singleton&lt;br&gt;
&amp;gt;subscripts&lt;br&gt;
&lt;br&gt;
try squeeze(M(:,:,q,:))&lt;br&gt;
&lt;br&gt;
M(:,:,q,:) is a 4D array whose third dimension happens to be 1.&lt;br&gt;
Matlab only automatically collapses trailing singleton array&lt;br&gt;
dimensions.&lt;br&gt;
-- &lt;br&gt;
&amp;nbsp;&amp;nbsp;&quot;Nothing recedes like success.&quot;             -- Walter Winchell</description>
    </item>
    <item>
      <pubDate>Mon, 21 Jul 2008 14:08:57 -0400</pubDate>
      <title>Re: vectorise a reshape loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172652#444317</link>
      <author>Peter Boettcher</author>
      <description>&quot;Dave Brackett&quot; &amp;lt;davebrackett@hotmail.com&amp;gt; writes:&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; figure out the dimensions of the right-hand side.  Try it first&lt;br&gt;
&amp;gt;&amp;gt; mentally, and if you're not sure, assign it to a variable and ask&lt;br&gt;
&amp;gt;&amp;gt; MATLAB to tell you what the size is.&lt;br&gt;
&amp;gt;&amp;gt; &lt;br&gt;
&amp;gt;&amp;gt; Then, look at where you want to assign it.  Your assignment indices&lt;br&gt;
&amp;gt;&amp;gt; must provide a place in the output which exactly matches the&lt;br&gt;
&amp;gt;&amp;gt; dimensions of the right side.&lt;br&gt;
&amp;gt;&amp;gt; &lt;br&gt;
&amp;gt;&amp;gt; For instance, I can't say:&lt;br&gt;
&amp;gt;&amp;gt; &lt;br&gt;
&amp;gt;&amp;gt; A(1, 2:4) = B(1:4, 2);&lt;br&gt;
&amp;gt;&amp;gt; &lt;br&gt;
&amp;gt;&amp;gt; Why?&lt;br&gt;
&amp;gt;&amp;gt; left side is 1x3, right side is 4x1&lt;br&gt;
&amp;gt;&amp;gt; &lt;br&gt;
&amp;gt;&amp;gt; I can't say:&lt;br&gt;
&amp;gt;&amp;gt; A(1,1,2:4) = B(1,2:4)&lt;br&gt;
&amp;gt;&amp;gt; &lt;br&gt;
&amp;gt;&amp;gt; Why?&lt;br&gt;
&amp;gt;&amp;gt; left side is 1x1x3, right side is 1x3&lt;br&gt;
&lt;br&gt;
Pay VERY careful to the examples above, again.  In the second example, I&lt;br&gt;
am not asking for the size of B, but for the size of entire right-hand&lt;br&gt;
side.  B is at least 1x4, but the indexing makes it 1x3.&lt;br&gt;
&lt;br&gt;
&amp;gt; ok, the size of my right side is 2x2x90x16 and the required&lt;br&gt;
&amp;gt; size for my left is 2x2x90x1 so I have written this as:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; prodH=zeros(2,2,length(freq),1);&lt;br&gt;
&amp;gt; for p=1:length(freq)&lt;br&gt;
&amp;gt;     q=p;&lt;br&gt;
&amp;gt;     prodH(:,:,q,1)=ndfun('mprod',M(:,:,q,:))&lt;br&gt;
&amp;gt; end&lt;br&gt;
&lt;br&gt;
The size of the left side IS NOT 2x2x90x1.  q is a scalar, so the left&lt;br&gt;
side is 2x2x1x1. The right side, I assume, is 2x2x1x16.  But don't guess&lt;br&gt;
or believe me.  If you can't figure out your dimension issues, I meant&lt;br&gt;
what I said about &quot;if you're not sure, assign it to a variable and ask&lt;br&gt;
MATLAB to tell you what the size is&quot;.&lt;br&gt;
&lt;br&gt;
tmp = ndfun('mprod', M(:,:,q,:));&lt;br&gt;
size(tmp)&lt;br&gt;
&lt;br&gt;
I could take a guess at fixing it, but only you know what you want&lt;br&gt;
where.  Actually, I think ndfun('mprod') is not doing what you think&lt;br&gt;
it's is doing.  Do what I suggest above, and I think you'll see what I&lt;br&gt;
mean.  Then go back and read the help for ndfun, carefully.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
-Peter</description>
    </item>
    <item>
      <pubDate>Mon, 21 Jul 2008 14:59:02 -0400</pubDate>
      <title>Re: vectorise a reshape loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172652#444323</link>
      <author>Dave Brackett</author>
      <description>&amp;gt; The size of the left side IS NOT 2x2x90x1. q is a scalar,&lt;br&gt;
&amp;gt; so the left side is 2x2x1x1. The right side, I assume, is&lt;br&gt;
&amp;gt; 2x2x1x16. But don't guess or believe me. If you can't &lt;br&gt;
&amp;gt; figure out your dimension issues, I meant what I said &lt;br&gt;
&amp;gt; about &quot;if you're not sure, assign it to a variable and &lt;br&gt;
&amp;gt; ask MATLAB to tell you what the size is&quot;.&lt;br&gt;
&amp;gt; tmp = ndfun('mprod', M(:,:,q,:));&lt;br&gt;
&amp;gt; size(tmp)&lt;br&gt;
&lt;br&gt;
I am getting quite confused with what I thought would be a&lt;br&gt;
simple thing. prodH and M are contained within a loop of 90&lt;br&gt;
iterations, so the size of the resulting prodH matrix after&lt;br&gt;
running the loop should be 2x2x90x1.&lt;br&gt;
&lt;br&gt;
M is of the size 2x2x90x16 and i am iterating through this&lt;br&gt;
using a loop to find the product of the 4th dimension&lt;br&gt;
(length 16) for each of the indices in the 3rd dimension&lt;br&gt;
(length 90). &lt;br&gt;
&lt;br&gt;
This would then result in prodH of the size 2x2x90x1 with&lt;br&gt;
each 2x2 matrix being the product of the 16 in M.&lt;br&gt;
&lt;br&gt;
I hope that makes sense. That is what I am trying to achieve&lt;br&gt;
and thought that ndfun('mprod') would be enable that. If my&lt;br&gt;
loop is confusing and seems to suggest something different&lt;br&gt;
then apologies.&lt;br&gt;
&lt;br&gt;
Please could you show me how to write this correctly as I am&lt;br&gt;
starting to go mad with it? Thanks.</description>
    </item>
    <item>
      <pubDate>Mon, 21 Jul 2008 15:16:55 -0400</pubDate>
      <title>Re: vectorise a reshape loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172652#444331</link>
      <author>Peter Boettcher</author>
      <description>&quot;Dave Brackett&quot; &amp;lt;davebrackett@hotmail.com&amp;gt; writes:&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; The size of the left side IS NOT 2x2x90x1. q is a scalar,&lt;br&gt;
&amp;gt;&amp;gt; so the left side is 2x2x1x1. The right side, I assume, is&lt;br&gt;
&amp;gt;&amp;gt; 2x2x1x16. But don't guess or believe me. If you can't &lt;br&gt;
&amp;gt;&amp;gt; figure out your dimension issues, I meant what I said &lt;br&gt;
&amp;gt;&amp;gt; about &quot;if you're not sure, assign it to a variable and &lt;br&gt;
&amp;gt;&amp;gt; ask MATLAB to tell you what the size is&quot;.&lt;br&gt;
&amp;gt;&amp;gt; tmp = ndfun('mprod', M(:,:,q,:));&lt;br&gt;
&amp;gt;&amp;gt; size(tmp)&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I am getting quite confused with what I thought would be a&lt;br&gt;
&amp;gt; simple thing. prodH and M are contained within a loop of 90&lt;br&gt;
&amp;gt; iterations, so the size of the resulting prodH matrix after&lt;br&gt;
&amp;gt; running the loop should be 2x2x90x1.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; M is of the size 2x2x90x16 and i am iterating through this&lt;br&gt;
&amp;gt; using a loop to find the product of the 4th dimension&lt;br&gt;
&amp;gt; (length 16) for each of the indices in the 3rd dimension&lt;br&gt;
&amp;gt; (length 90). &lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; This would then result in prodH of the size 2x2x90x1 with&lt;br&gt;
&amp;gt; each 2x2 matrix being the product of the 16 in M.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I hope that makes sense. That is what I am trying to achieve&lt;br&gt;
&amp;gt; and thought that ndfun('mprod') would be enable that. If my&lt;br&gt;
&amp;gt; loop is confusing and seems to suggest something different&lt;br&gt;
&amp;gt; then apologies.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Please could you show me how to write this correctly as I am&lt;br&gt;
&amp;gt; starting to go mad with it? Thanks.&lt;br&gt;
&lt;br&gt;
Until now, I have been unable to understand exactly what you want where.&lt;br&gt;
With this explanation, I think I can help.&lt;br&gt;
&lt;br&gt;
I admit that I am still frustrated that you refuse to follow my&lt;br&gt;
debugging advice by looking at the size of the matrix generated by&lt;br&gt;
ndfun, and then reading the help for ndfun.  So instead of just writing&lt;br&gt;
the answer, I'll go through the exercise here, hopefully providing an&lt;br&gt;
illustration of the debugging process I was asking you to try.&lt;br&gt;
&lt;br&gt;
M = rand(2,2,90,16);&lt;br&gt;
tmp = ndfun('mprod', M(:,:,1,:));&lt;br&gt;
size(tmp)&lt;br&gt;
&lt;br&gt;
ans = &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;2  2  16&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Hmm, why is it 2x2x16?&lt;br&gt;
&lt;br&gt;
&quot;help ndfun&quot;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'mprod' behaves differently.  It cumulatively multiplies a set&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;of matrices and produces a single 2D output.  The equivalent&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;code is:&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;C = A(:,:,1);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for i=2:N&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;C = C * A(:,:,i);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2D inputs return themselves.  Inputs with more than 3&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dimensions collapse the 3rd dimension only.  So with an A of&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;size [2 2 7 3 4],&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;C = ndfun('mprod', A);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;is equivalent to&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for i=1:3&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for j=1:4&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;C(:,:,i,j)=ndfun('mprod',M(:,:,:,i,j));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;and C will have size [2 2 3 4].&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Reading that carefully, especially the second example, suggests that&lt;br&gt;
inputting a 2x2x1x16 matrix will produce a 2x2x16 output, essentially&lt;br&gt;
doing nothing.  Since ndfun('mprod') works down the third dimension, you&lt;br&gt;
want your x16 size to be on that third dimension.  &quot;Squeeze&quot; will do&lt;br&gt;
that in this case, but it's just a special case of permute, so whichever&lt;br&gt;
you want.&lt;br&gt;
&lt;br&gt;
So this should work:&lt;br&gt;
&lt;br&gt;
prodH=zeros(2,2,length(freq),1);&lt;br&gt;
for p=1:length(freq)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;q=p;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;prodH(:,:,q,1)=ndfun('mprod', squeeze(M(:,:,q,:)))&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
Let's check:&lt;br&gt;
&lt;br&gt;
Right side:&lt;br&gt;
&lt;br&gt;
M(:,:,q,:) is 2x2x1x16&lt;br&gt;
squeeze(M(:,:,q,:)) is 2x2x16&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;(you can always ignore trailing 1 dimensions)&lt;br&gt;
output of ndfun is 2x2&lt;br&gt;
&lt;br&gt;
Left side:&lt;br&gt;
prodH(:,:,q,1) is 2x2&lt;br&gt;
&lt;br&gt;
Good!&lt;br&gt;
&lt;br&gt;
OK, one more thing.  Again looking carefully at the help for ndfun, the&lt;br&gt;
example shows a case where the mprod is repeated for higher-dimension&lt;br&gt;
indices.  So it appears that if you put your x90 dimension at the end&lt;br&gt;
(in the fourth dimension), ndfun will automatically do what you want,&lt;br&gt;
which is to collapse the 3rd dimension (16) by multiplying the 2x2&lt;br&gt;
matrices, then do this for all of the 4d &quot;pages&quot;.  So you want to switch&lt;br&gt;
3rd and 4th dimensions of M:&lt;br&gt;
&lt;br&gt;
This should do everything, sans loop:&lt;br&gt;
&lt;br&gt;
prodH = ndfun('mprod', permute(M, [1 2 4 3]));&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
-Peter</description>
    </item>
    <item>
      <pubDate>Tue, 22 Jul 2008 15:46:03 -0400</pubDate>
      <title>Re: vectorise a reshape loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/172652#444641</link>
      <author>Dave Brackett</author>
      <description>Thank you for your clear explanation and help with this. I&lt;br&gt;
appreciate your time and effort. </description>
    </item>
  </channel>
</rss>

