<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239149</link>
    <title>MATLAB Central Newsreader - MATLAB Reshape Challenge: N x d matrix to N/K x d x K matrix</title>
    <description>Feed for thread: MATLAB Reshape Challenge: N x d matrix to N/K x d x K matrix</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>Thu, 13 Nov 2008 00:33:39 -0500</pubDate>
      <title>MATLAB Reshape Challenge: N x d matrix to N/K x d x K matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239149#610604</link>
      <author>mprocopio@gmail.com</author>
      <description>Hi folks,&lt;br&gt;
&lt;br&gt;
I'm working on parallelizing some machine learning code in MATLAB. I'm&lt;br&gt;
using the Parallel Computing Toolbox and the parfor construct;&lt;br&gt;
therefore, I have certain restrictions on how I must &quot;slice&quot; into&lt;br&gt;
certain data structures accessed within the parfor (parallel for)&lt;br&gt;
loop.&lt;br&gt;
&lt;br&gt;
Basically, I need to reshape my training data from an (N x d) two-&lt;br&gt;
dimensional matrix to a fully &quot;slicable&quot; (N/K x d x K) matrix, where K&lt;br&gt;
is the number of available parallel threads (which corresponds to the&lt;br&gt;
number of &quot;slicable&quot; input data partitions). Here, I would slice among&lt;br&gt;
the third dimension, i.e., partitioned_data&lt;br&gt;
(:,:,parallel_loop_index_i).&lt;br&gt;
&lt;br&gt;
(For now, assume N is a multiple of K.)&lt;br&gt;
&lt;br&gt;
I'm usually very good with reshape, permute, shiftdim, etc., but I am&lt;br&gt;
having trouble making this work nicely.&lt;br&gt;
&lt;br&gt;
I could hack in something like slicable_data_partitions = cat(3,&lt;br&gt;
manual_partition_1, manual_partition_2, ..., manual_partition_K) but&lt;br&gt;
that's horrible and I was hoping for an efficient one-liner with some&lt;br&gt;
combination of reshape, permute, etc. Performance is important, as&lt;br&gt;
this will be fairly large scale, i.e., numel(data) on the order of&lt;br&gt;
10^7.&lt;br&gt;
&lt;br&gt;
Note I can actually get reshape to return output int he desired&lt;br&gt;
dimension, however, the d data elements along the second dimension in&lt;br&gt;
each of the N rows are no longer in the correct order.&lt;br&gt;
&lt;br&gt;
Detailed example of desired output:&lt;br&gt;
&lt;br&gt;
[N x d, 8 x 3]:&lt;br&gt;
&lt;br&gt;
input_data = reshape(1:24, 3, 8)'   % Note transpose&lt;br&gt;
&lt;br&gt;
input_data =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1     2     3&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4     5     6&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;7     8     9&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;10    11    12&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;13    14    15&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;16    17    18&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;19    20    21&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;22    23    24&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
For K = 4, [N/K x d x K, 2 x 3 x 4]&lt;br&gt;
&lt;br&gt;
desired_transformed_data =&lt;br&gt;
&lt;br&gt;
ans(:,:,1) =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1     2    3&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4    5    6&lt;br&gt;
&lt;br&gt;
ans(:,:,2) =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;7    8     9&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;10     11    12&lt;br&gt;
&lt;br&gt;
ans(:,:,3) =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;13    14     15&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;16    17     18&lt;br&gt;
&lt;br&gt;
ans(:,:,4) =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;19    20    21&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;22    23    24&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Any help would be greatly appreciated.&lt;br&gt;
&lt;br&gt;
On a final note, from a memory performance standpoint: any performance&lt;br&gt;
difference in the dimension along which the data is sliced? I.e.,&lt;br&gt;
performance difference for [N/K x d x K] and slicing along the third&lt;br&gt;
dimension, versus [K x d x N/K] and slicing along the first dimension?&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
With thanks,&lt;br&gt;
&lt;br&gt;
--Mike</description>
    </item>
    <item>
      <pubDate>Thu, 13 Nov 2008 01:23:02 -0500</pubDate>
      <title>Re: MATLAB Reshape Challenge: N x d matrix to N/K x d x K matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239149#610609</link>
      <author>Nick Denman</author>
      <description>mprocopio@gmail.com wrote in message &amp;lt;f6776e56-5088-4685-85ee-fa28328610e6@v4g2000yqa.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi folks,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I'm working on parallelizing some machine learning code in MATLAB. I'm&lt;br&gt;
&amp;gt; using the Parallel Computing Toolbox and the parfor construct;&lt;br&gt;
&amp;gt; therefore, I have certain restrictions on how I must &quot;slice&quot; into&lt;br&gt;
&amp;gt; certain data structures accessed within the parfor (parallel for)&lt;br&gt;
&amp;gt; loop.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Basically, I need to reshape my training data from an (N x d) two-&lt;br&gt;
&amp;gt; dimensional matrix to a fully &quot;slicable&quot; (N/K x d x K) matrix, where K&lt;br&gt;
&amp;gt; is the number of available parallel threads (which corresponds to the&lt;br&gt;
&amp;gt; number of &quot;slicable&quot; input data partitions). Here, I would slice among&lt;br&gt;
&amp;gt; the third dimension, i.e., partitioned_data&lt;br&gt;
&amp;gt; (:,:,parallel_loop_index_i).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; (For now, assume N is a multiple of K.)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I'm usually very good with reshape, permute, shiftdim, etc., but I am&lt;br&gt;
&amp;gt; having trouble making this work nicely.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I could hack in something like slicable_data_partitions = cat(3,&lt;br&gt;
&amp;gt; manual_partition_1, manual_partition_2, ..., manual_partition_K) but&lt;br&gt;
&amp;gt; that's horrible and I was hoping for an efficient one-liner with some&lt;br&gt;
&amp;gt; combination of reshape, permute, etc. Performance is important, as&lt;br&gt;
&amp;gt; this will be fairly large scale, i.e., numel(data) on the order of&lt;br&gt;
&amp;gt; 10^7.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Note I can actually get reshape to return output int he desired&lt;br&gt;
&amp;gt; dimension, however, the d data elements along the second dimension in&lt;br&gt;
&amp;gt; each of the N rows are no longer in the correct order.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Detailed example of desired output:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; [N x d, 8 x 3]:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; input_data = reshape(1:24, 3, 8)'   % Note transpose&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; input_data =&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;      1     2     3&lt;br&gt;
&amp;gt;      4     5     6&lt;br&gt;
&amp;gt;      7     8     9&lt;br&gt;
&amp;gt;     10    11    12&lt;br&gt;
&amp;gt;     13    14    15&lt;br&gt;
&amp;gt;     16    17    18&lt;br&gt;
&amp;gt;     19    20    21&lt;br&gt;
&amp;gt;     22    23    24&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; For K = 4, [N/K x d x K, 2 x 3 x 4]&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; desired_transformed_data =&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; ans(:,:,1) =&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;      1     2    3&lt;br&gt;
&amp;gt;      4    5    6&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; ans(:,:,2) =&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;     7    8     9&lt;br&gt;
&amp;gt;     10     11    12&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; ans(:,:,3) =&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;     13    14     15&lt;br&gt;
&amp;gt;     16    17     18&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; ans(:,:,4) =&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;      19    20    21&lt;br&gt;
&amp;gt;     22    23    24&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Any help would be greatly appreciated.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; On a final note, from a memory performance standpoint: any performance&lt;br&gt;
&amp;gt; difference in the dimension along which the data is sliced? I.e.,&lt;br&gt;
&amp;gt; performance difference for [N/K x d x K] and slicing along the third&lt;br&gt;
&amp;gt; dimension, versus [K x d x N/K] and slicing along the first dimension?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; With thanks,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; --Mike&lt;br&gt;
&lt;br&gt;
Hi Mike&lt;br&gt;
&lt;br&gt;
Try the following:&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; input_data = [1 2 3&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4 5 6&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;7 8 9&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;10 11 12&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;13 14 15&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;16 17 18&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;19 20 21&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;22 23 24];&lt;br&gt;
&amp;gt;&amp;gt; [N d] = size(input_data);&lt;br&gt;
&amp;gt;&amp;gt; K = 4;&lt;br&gt;
&amp;gt;&amp;gt; permuted_data = permute(reshape(permute(input_data,[2 1]),[d N/K K]),[2 1 3])&lt;br&gt;
&lt;br&gt;
HTH,&lt;br&gt;
Nick</description>
    </item>
    <item>
      <pubDate>Thu, 13 Nov 2008 19:27:09 -0500</pubDate>
      <title>Re: MATLAB Reshape Challenge: N x d matrix to N/K x d x K matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239149#610758</link>
      <author>mprocopio@gmail.com</author>
      <description>Awesome, Nick, thank you so much.&lt;br&gt;
&lt;br&gt;
That completely solved the problem, and appears very efficient (0.25s&lt;br&gt;
processing time for K=32, d=87, and N=105504).&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Thanks again--I very much appreciate it!&lt;br&gt;
&lt;br&gt;
--Mike</description>
    </item>
  </channel>
</rss>

