<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153352</link>
    <title>MATLAB Central Newsreader - how to insert/delete rows in a matrix without copy the rest of matrix</title>
    <description>Feed for thread: how to insert/delete rows in a matrix without copy the rest of 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>Wed, 25 Jul 2007 13:50:59 -0400</pubDate>
      <title>how to insert/delete rows in a matrix without copy the rest of matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153352#384818</link>
      <author>Chenyang </author>
      <description>Matlab is very slow add/delete a row in a matrix.&lt;br&gt;
It always trying to copy the whole matrix&lt;br&gt;
e.g., &lt;br&gt;
&lt;br&gt;
a = rand(5000);&lt;br&gt;
tic&lt;br&gt;
a(:,1) = [];&lt;br&gt;
toc&lt;br&gt;
tic&lt;br&gt;
a = [a(:,1),a];&lt;br&gt;
toc&lt;br&gt;
Elapsed time is 0.447241 seconds.&lt;br&gt;
Elapsed time is 0.556416 seconds.&lt;br&gt;
&lt;br&gt;
I need to insert/delete a row into a large matrix.&lt;br&gt;
How to make it fast?</description>
    </item>
    <item>
      <pubDate>Wed, 25 Jul 2007 15:03:46 -0400</pubDate>
      <title>Re: how to insert/delete rows in a matrix without copy the rest of matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153352#384834</link>
      <author>Daphne W</author>
      <description>&lt;br&gt;
The speed of the calculation partially depends on the system you're workin on. &lt;br&gt;
On my laptop got &lt;br&gt;
Elapsed time is 0.415618 seconds.&lt;br&gt;
Elapsed time is 0.415288 seconds.&lt;br&gt;
For your code.&lt;br&gt;
&lt;br&gt;
Trying to do this faster, I simply redefined the a matrix without the first column as you have: &lt;br&gt;
&lt;br&gt;
a=rand(5000);&lt;br&gt;
tic; a=a(:,2:end); toc&lt;br&gt;
Elapsed time is 0.250436 seconds.&lt;br&gt;
Not great, but a bit better. &lt;br&gt;
&lt;br&gt;
Daphne&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&quot;Chenyang &quot; &amp;lt;john.doe.nospam@mathworks.com&amp;gt; wrote in &lt;br&gt;
message &amp;lt;f87kg3$9ri$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Matlab is very slow add/delete a row in a matrix.&lt;br&gt;
&amp;gt; It always trying to copy the whole matrix&lt;br&gt;
&amp;gt; e.g., &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; a = rand(5000);&lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; a(:,1) = [];&lt;br&gt;
&amp;gt; toc&lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; a = [a(:,1),a];&lt;br&gt;
&amp;gt; toc&lt;br&gt;
&amp;gt; Elapsed time is 0.447241 seconds.&lt;br&gt;
&amp;gt; Elapsed time is 0.556416 seconds.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I need to insert/delete a row into a large matrix.&lt;br&gt;
&amp;gt; How to make it fast?&lt;br&gt;
&amp;gt; </description>
    </item>
    <item>
      <pubDate>Wed, 25 Jul 2007 16:20:01 -0400</pubDate>
      <title>Re: how to insert/delete rows in a matrix without copy the rest of matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153352#384855</link>
      <author>Chenyang </author>
      <description>Thank you, but this is not a &quot;true&quot; solution.  The codes are just for illustration of the problem.  What if you want to delete/add the 99th row?  &lt;br&gt;
&lt;br&gt;
The problem lies Matlab copying whole matrix while actually only a very small portion of the matrix being modified.&lt;br&gt;
I guess to really solve this problem we have to find a data-structure that allows a matrix to span multiple memory segments and are somehow linked together by indexing pointers. &lt;br&gt;
&lt;br&gt;
&quot;Daphne W&quot; &amp;lt;daphnew_too_nospam@yahoo.com&amp;gt; wrote in message &amp;lt;f87ooi$rma$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The speed of the calculation partially depends on the system you're workin on. &lt;br&gt;
&amp;gt; On my laptop got &lt;br&gt;
&amp;gt; Elapsed time is 0.415618 seconds.&lt;br&gt;
&amp;gt; Elapsed time is 0.415288 seconds.&lt;br&gt;
&amp;gt; For your code.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Trying to do this faster, I simply redefined the a matrix without the first column as you have: &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; a=rand(5000);&lt;br&gt;
&amp;gt; tic; a=a(:,2:end); toc&lt;br&gt;
&amp;gt; Elapsed time is 0.250436 seconds.&lt;br&gt;
&amp;gt; Not great, but a bit better. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Daphne&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &quot;Chenyang &quot; &amp;lt;john.doe.nospam@mathworks.com&amp;gt; wrote in &lt;br&gt;
&amp;gt; message &amp;lt;f87kg3$9ri$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Matlab is very slow add/delete a row in a matrix.&lt;br&gt;
&amp;gt; &amp;gt; It always trying to copy the whole matrix&lt;br&gt;
&amp;gt; &amp;gt; e.g., &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; a = rand(5000);&lt;br&gt;
&amp;gt; &amp;gt; tic&lt;br&gt;
&amp;gt; &amp;gt; a(:,1) = [];&lt;br&gt;
&amp;gt; &amp;gt; toc&lt;br&gt;
&amp;gt; &amp;gt; tic&lt;br&gt;
&amp;gt; &amp;gt; a = [a(:,1),a];&lt;br&gt;
&amp;gt; &amp;gt; toc&lt;br&gt;
&amp;gt; &amp;gt; Elapsed time is 0.447241 seconds.&lt;br&gt;
&amp;gt; &amp;gt; Elapsed time is 0.556416 seconds.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I need to insert/delete a row into a large matrix.&lt;br&gt;
&amp;gt; &amp;gt; How to make it fast?&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; </description>
    </item>
    <item>
      <pubDate>Wed, 25 Jul 2007 16:43:01 -0400</pubDate>
      <title>Re: how to insert/delete rows in a matrix without copy the rest of matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153352#384868</link>
      <author>Tim Davis</author>
      <description>&quot;Chenyang &quot; &amp;lt;john.doe.nospam@mathworks.com&amp;gt; wrote in message &amp;lt;f87t7h$mje$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Thank you, but this is not a &quot;true&quot; solution.  The codes are just for illustration of the problem.  What if you want to delete/add the 99th row?  &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The problem lies Matlab copying whole matrix while actually only a very small portion of the matrix being modified.&lt;br&gt;
&amp;gt; I guess to really solve this problem we have to find a data-structure that allows a matrix to span multiple memory segments and are somehow linked together by indexing pointers. &lt;br&gt;
&amp;gt;&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
MATLAB stores a dense matrix in an m-by-n array, in column major form, without pointers.  Using pointers would cause a dreadful slowdown in any matrix computations other than&lt;br&gt;
&lt;br&gt;
a (:,1) = [ ] ;&lt;br&gt;
&lt;br&gt;
which is why no pointers are used (the BLAS do not use pointers).&lt;br&gt;
&lt;br&gt;
So the problem is intrinsically difficult.&lt;br&gt;
&lt;br&gt;
Your best solution would be to avoid the need to delete rows/columns at all.  Can you set the row or column to delete to zero, for example?&lt;br&gt;
&lt;br&gt;
Another alternative would be to keep a list of rows and columns that have not been &quot;deleted&quot;, but not to change a itself.  Try this:&lt;br&gt;
&lt;br&gt;
a = rand (5000) ;&lt;br&gt;
&lt;br&gt;
delrow = 99 ;&lt;br&gt;
&lt;br&gt;
tic&lt;br&gt;
b = [a(1:delrow-1,:) ; a(delrow+1:end,:)] ;&lt;br&gt;
toc&lt;br&gt;
&lt;br&gt;
tic&lt;br&gt;
a (delrow,:) = [ ];&lt;br&gt;
toc&lt;br&gt;
&lt;br&gt;
[m n] = size (a)&lt;br&gt;
p = logical (ones (m, 1)) ;&lt;br&gt;
q = logical (ones (1, n)) ;&lt;br&gt;
nrows = m ;&lt;br&gt;
ncols = n ;&lt;br&gt;
&lt;br&gt;
% to delete a row in a:&lt;br&gt;
p (delrow) = 0 ;&lt;br&gt;
nrows = nrows - 1 ;&lt;br&gt;
&lt;br&gt;
% to delete a colum in a:&lt;br&gt;
q (101) = 0 ;&lt;br&gt;
ncols = ncols - 1 ;&lt;br&gt;
&lt;br&gt;
tic&lt;br&gt;
b = a(p,q) ;&lt;br&gt;
toc&lt;br&gt;
&lt;br&gt;
% try using a(p,q) explicitly:&lt;br&gt;
x = rand (ncols,1) ;&lt;br&gt;
tic&lt;br&gt;
y = a(p,q) * x ;&lt;br&gt;
toc&lt;br&gt;
&lt;br&gt;
tic&lt;br&gt;
y = b*x ;&lt;br&gt;
toc&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
The logical indexing is a little faster.</description>
    </item>
    <item>
      <pubDate>Wed, 25 Jul 2007 17:51:32 -0400</pubDate>
      <title>Re: how to insert/delete rows in a matrix without copy the rest of matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153352#384893</link>
      <author>Robert </author>
      <description>&lt;br&gt;
do you know a priori how large the matrix will be (or at least estimate an upper bound)?  pre-allocating large structures is generally the solution for problems like this.</description>
    </item>
    <item>
      <pubDate>Thu, 08 Nov 2007 14:37:47 -0500</pubDate>
      <title>Re: how to insert/delete rows in a matrix without copy the rest of matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153352#400353</link>
      <author>Sterren Latsky</author>
      <description>Hi guys. I have a similar query.&lt;br&gt;
&lt;br&gt;
&amp;gt; MATLAB stores a dense matrix in an m-by-n array, in column&lt;br&gt;
major form, without pointers.  Using pointers would cause a&lt;br&gt;
dreadful slowdown in any matrix computations other than&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; a (:,1) = [ ] ;&lt;br&gt;
&lt;br&gt;
I want to delete a row and column in an arbitrary position&lt;br&gt;
in a very large matrix (up to 2000 rows / columns). The&lt;br&gt;
matrix is a sparse matrix, so I am assuming it uses pointers&lt;br&gt;
to non zero elements in rows and columns.&lt;br&gt;
Theoretically, if it does this, there must be an easier way&lt;br&gt;
to delete a row or column. Does anyone know how?&lt;br&gt;
&lt;br&gt;
Thanks,&lt;br&gt;
&lt;br&gt;
Sterren</description>
    </item>
    <item>
      <pubDate>Fri, 16 Nov 2007 02:30:02 -0500</pubDate>
      <title>Re: how to insert/delete rows in a matrix without copy the rest of matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153352#401606</link>
      <author>Tim Davis</author>
      <description>&quot;Sterren Latsky&quot; &amp;lt;stez17@hotmail.com&amp;gt; wrote in message&lt;br&gt;
&amp;lt;fgv6vr$d7c$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi guys. I have a similar query.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; MATLAB stores a dense matrix in an m-by-n array, in column&lt;br&gt;
&amp;gt; major form, without pointers.  Using pointers would cause a&lt;br&gt;
&amp;gt; dreadful slowdown in any matrix computations other than&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; a (:,1) = [ ] ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I want to delete a row and column in an arbitrary position&lt;br&gt;
&amp;gt; in a very large matrix (up to 2000 rows / columns). The&lt;br&gt;
&amp;gt; matrix is a sparse matrix, so I am assuming it uses pointers&lt;br&gt;
&amp;gt; to non zero elements in rows and columns.&lt;br&gt;
&amp;gt; Theoretically, if it does this, there must be an easier way&lt;br&gt;
&amp;gt; to delete a row or column. Does anyone know how?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Sterren&lt;br&gt;
&lt;br&gt;
Check out Loren's March 1st blog for details of how MATLAB&lt;br&gt;
stores its sparse matrices.&lt;br&gt;
&lt;br&gt;
MATLAB stores its sparse matrices by column.  Deleting a row&lt;br&gt;
or column requires a complete copy of the matrix, even if&lt;br&gt;
you were to do it in a C mexFunction.  The data structure is&lt;br&gt;
designed for fast whole-matrix operations, not for the (less&lt;br&gt;
frequently used) a(:,1)=[] kind of operation.</description>
    </item>
    <item>
      <pubDate>Sun, 18 Nov 2007 00:10:58 -0500</pubDate>
      <title>Re: how to insert/delete rows in a matrix without copy the rest of matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153352#401893</link>
      <author>Kevin Lin</author>
      <description>If I want to delete arbitrary m rows (for simplicity, say&lt;br&gt;
m=2 and 3) in a matrix, what can I do? Thanks if any of you&lt;br&gt;
can help.&lt;br&gt;
&lt;br&gt;
&quot;Tim Davis&quot; &amp;lt;davis@cise.ufl.edu&amp;gt; wrote in message&lt;br&gt;
&amp;lt;f87uil$ggh$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Chenyang &quot; &amp;lt;john.doe.nospam@mathworks.com&amp;gt; wrote in&lt;br&gt;
message &amp;lt;f87t7h$mje$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Thank you, but this is not a &quot;true&quot; solution.  The codes&lt;br&gt;
are just for illustration of the problem.  What if you want&lt;br&gt;
to delete/add the 99th row?  &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; The problem lies Matlab copying whole matrix while&lt;br&gt;
actually only a very small portion of the matrix being modified.&lt;br&gt;
&amp;gt; &amp;gt; I guess to really solve this problem we have to find a&lt;br&gt;
data-structure that allows a matrix to span multiple memory&lt;br&gt;
segments and are somehow linked together by indexing pointers. &lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; MATLAB stores a dense matrix in an m-by-n array, in column&lt;br&gt;
major form, without pointers.  Using pointers would cause a&lt;br&gt;
dreadful slowdown in any matrix computations other than&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; a (:,1) = [ ] ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; which is why no pointers are used (the BLAS do not use&lt;br&gt;
pointers).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; So the problem is intrinsically difficult.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Your best solution would be to avoid the need to delete&lt;br&gt;
rows/columns at all.  Can you set the row or column to&lt;br&gt;
delete to zero, for example?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Another alternative would be to keep a list of rows and&lt;br&gt;
columns that have not been &quot;deleted&quot;, but not to change a&lt;br&gt;
itself.  Try this:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; a = rand (5000) ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; delrow = 99 ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; b = [a(1:delrow-1,:) ; a(delrow+1:end,:)] ;&lt;br&gt;
&amp;gt; toc&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; a (delrow,:) = [ ];&lt;br&gt;
&amp;gt; toc&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; [m n] = size (a)&lt;br&gt;
&amp;gt; p = logical (ones (m, 1)) ;&lt;br&gt;
&amp;gt; q = logical (ones (1, n)) ;&lt;br&gt;
&amp;gt; nrows = m ;&lt;br&gt;
&amp;gt; ncols = n ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % to delete a row in a:&lt;br&gt;
&amp;gt; p (delrow) = 0 ;&lt;br&gt;
&amp;gt; nrows = nrows - 1 ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % to delete a colum in a:&lt;br&gt;
&amp;gt; q (101) = 0 ;&lt;br&gt;
&amp;gt; ncols = ncols - 1 ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; b = a(p,q) ;&lt;br&gt;
&amp;gt; toc&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % try using a(p,q) explicitly:&lt;br&gt;
&amp;gt; x = rand (ncols,1) ;&lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; y = a(p,q) * x ;&lt;br&gt;
&amp;gt; toc&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; y = b*x ;&lt;br&gt;
&amp;gt; toc&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The logical indexing is a little faster.</description>
    </item>
    <item>
      <pubDate>Sun, 18 Nov 2007 00:14:41 -0500</pubDate>
      <title>Re: how to insert/delete rows in a matrix without copy the rest of matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153352#401894</link>
      <author>Kevin Lin</author>
      <description>If I want to delete arbitrary m rows (for simplicity, say&lt;br&gt;
m=2 and 3) in a matrix, what can I do? Thanks if any of you&lt;br&gt;
can help.&lt;br&gt;
&lt;br&gt;
&quot;Tim Davis&quot; &amp;lt;davis@cise.ufl.edu&amp;gt; wrote in message&lt;br&gt;
&amp;lt;f87uil$ggh$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Chenyang &quot; &amp;lt;john.doe.nospam@mathworks.com&amp;gt; wrote in&lt;br&gt;
message &amp;lt;f87t7h$mje$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Thank you, but this is not a &quot;true&quot; solution.  The codes&lt;br&gt;
are just for illustration of the problem.  What if you want&lt;br&gt;
to delete/add the 99th row?  &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; The problem lies Matlab copying whole matrix while&lt;br&gt;
actually only a very small portion of the matrix being modified.&lt;br&gt;
&amp;gt; &amp;gt; I guess to really solve this problem we have to find a&lt;br&gt;
data-structure that allows a matrix to span multiple memory&lt;br&gt;
segments and are somehow linked together by indexing pointers. &lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; MATLAB stores a dense matrix in an m-by-n array, in column&lt;br&gt;
major form, without pointers.  Using pointers would cause a&lt;br&gt;
dreadful slowdown in any matrix computations other than&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; a (:,1) = [ ] ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; which is why no pointers are used (the BLAS do not use&lt;br&gt;
pointers).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; So the problem is intrinsically difficult.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Your best solution would be to avoid the need to delete&lt;br&gt;
rows/columns at all.  Can you set the row or column to&lt;br&gt;
delete to zero, for example?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Another alternative would be to keep a list of rows and&lt;br&gt;
columns that have not been &quot;deleted&quot;, but not to change a&lt;br&gt;
itself.  Try this:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; a = rand (5000) ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; delrow = 99 ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; b = [a(1:delrow-1,:) ; a(delrow+1:end,:)] ;&lt;br&gt;
&amp;gt; toc&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; a (delrow,:) = [ ];&lt;br&gt;
&amp;gt; toc&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; [m n] = size (a)&lt;br&gt;
&amp;gt; p = logical (ones (m, 1)) ;&lt;br&gt;
&amp;gt; q = logical (ones (1, n)) ;&lt;br&gt;
&amp;gt; nrows = m ;&lt;br&gt;
&amp;gt; ncols = n ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % to delete a row in a:&lt;br&gt;
&amp;gt; p (delrow) = 0 ;&lt;br&gt;
&amp;gt; nrows = nrows - 1 ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % to delete a colum in a:&lt;br&gt;
&amp;gt; q (101) = 0 ;&lt;br&gt;
&amp;gt; ncols = ncols - 1 ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; b = a(p,q) ;&lt;br&gt;
&amp;gt; toc&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % try using a(p,q) explicitly:&lt;br&gt;
&amp;gt; x = rand (ncols,1) ;&lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; y = a(p,q) * x ;&lt;br&gt;
&amp;gt; toc&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; y = b*x ;&lt;br&gt;
&amp;gt; toc&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The logical indexing is a little faster.</description>
    </item>
    <item>
      <pubDate>Sun, 18 Nov 2007 01:39:04 -0500</pubDate>
      <title>Re: how to insert/delete rows in a matrix without copy the rest of matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153352#401895</link>
      <author>Kevin Lin</author>
      <description>More precisely, suppose that matrix A has n rows. I want to&lt;br&gt;
get all sub-matrices (the total number of sub-matrices&lt;br&gt;
should be m choosing from n) by removing m rows from A.&lt;br&gt;
Again, for simplicity, we can say m=2 or 3. Is there any&lt;br&gt;
good way to code it in Matlab? Thanks.&lt;br&gt;
&lt;br&gt;
&quot;Kevin Lin&quot; &amp;lt;klin@mathworks.com&amp;gt; wrote in message&lt;br&gt;
&amp;lt;fho05h$3be$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; If I want to delete arbitrary m rows (for simplicity, say&lt;br&gt;
&amp;gt; m=2 and 3) in a matrix, what can I do? Thanks if any of you&lt;br&gt;
&amp;gt; can help.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &quot;Tim Davis&quot; &amp;lt;davis@cise.ufl.edu&amp;gt; wrote in message&lt;br&gt;
&amp;gt; &amp;lt;f87uil$ggh$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; &quot;Chenyang &quot; &amp;lt;john.doe.nospam@mathworks.com&amp;gt; wrote in&lt;br&gt;
&amp;gt; message &amp;lt;f87t7h$mje$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Thank you, but this is not a &quot;true&quot; solution.  The codes&lt;br&gt;
&amp;gt; are just for illustration of the problem.  What if you want&lt;br&gt;
&amp;gt; to delete/add the 99th row?  &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; The problem lies Matlab copying whole matrix while&lt;br&gt;
&amp;gt; actually only a very small portion of the matrix being&lt;br&gt;
modified.&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; I guess to really solve this problem we have to find a&lt;br&gt;
&amp;gt; data-structure that allows a matrix to span multiple memory&lt;br&gt;
&amp;gt; segments and are somehow linked together by indexing&lt;br&gt;
pointers. &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; MATLAB stores a dense matrix in an m-by-n array, in column&lt;br&gt;
&amp;gt; major form, without pointers.  Using pointers would cause a&lt;br&gt;
&amp;gt; dreadful slowdown in any matrix computations other than&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; a (:,1) = [ ] ;&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; which is why no pointers are used (the BLAS do not use&lt;br&gt;
&amp;gt; pointers).&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; So the problem is intrinsically difficult.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Your best solution would be to avoid the need to delete&lt;br&gt;
&amp;gt; rows/columns at all.  Can you set the row or column to&lt;br&gt;
&amp;gt; delete to zero, for example?&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Another alternative would be to keep a list of rows and&lt;br&gt;
&amp;gt; columns that have not been &quot;deleted&quot;, but not to change a&lt;br&gt;
&amp;gt; itself.  Try this:&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; a = rand (5000) ;&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; delrow = 99 ;&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; tic&lt;br&gt;
&amp;gt; &amp;gt; b = [a(1:delrow-1,:) ; a(delrow+1:end,:)] ;&lt;br&gt;
&amp;gt; &amp;gt; toc&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; tic&lt;br&gt;
&amp;gt; &amp;gt; a (delrow,:) = [ ];&lt;br&gt;
&amp;gt; &amp;gt; toc&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; [m n] = size (a)&lt;br&gt;
&amp;gt; &amp;gt; p = logical (ones (m, 1)) ;&lt;br&gt;
&amp;gt; &amp;gt; q = logical (ones (1, n)) ;&lt;br&gt;
&amp;gt; &amp;gt; nrows = m ;&lt;br&gt;
&amp;gt; &amp;gt; ncols = n ;&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; % to delete a row in a:&lt;br&gt;
&amp;gt; &amp;gt; p (delrow) = 0 ;&lt;br&gt;
&amp;gt; &amp;gt; nrows = nrows - 1 ;&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; % to delete a colum in a:&lt;br&gt;
&amp;gt; &amp;gt; q (101) = 0 ;&lt;br&gt;
&amp;gt; &amp;gt; ncols = ncols - 1 ;&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; tic&lt;br&gt;
&amp;gt; &amp;gt; b = a(p,q) ;&lt;br&gt;
&amp;gt; &amp;gt; toc&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; % try using a(p,q) explicitly:&lt;br&gt;
&amp;gt; &amp;gt; x = rand (ncols,1) ;&lt;br&gt;
&amp;gt; &amp;gt; tic&lt;br&gt;
&amp;gt; &amp;gt; y = a(p,q) * x ;&lt;br&gt;
&amp;gt; &amp;gt; toc&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; tic&lt;br&gt;
&amp;gt; &amp;gt; y = b*x ;&lt;br&gt;
&amp;gt; &amp;gt; toc&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; The logical indexing is a little faster.&lt;br&gt;
&amp;gt; </description>
    </item>
    <item>
      <pubDate>Mon, 21 Jul 2008 23:04:05 -0400</pubDate>
      <title>Re: how to insert/delete rows in a matrix without copy the rest of matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153352#444441</link>
      <author>Jens Christiansen</author>
      <description>&quot;Chenyang &quot; &amp;lt;john.doe.nospam@mathworks.com&amp;gt; wrote in message&lt;br&gt;
&amp;lt;f87kg3$9ri$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Matlab is very slow add/delete a row in a matrix.&lt;br&gt;
&amp;gt; It always trying to copy the whole matrix&lt;br&gt;
&amp;gt; e.g., &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; a = rand(5000);&lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; a(:,1) = [];&lt;br&gt;
&amp;gt; toc&lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; a = [a(:,1),a];&lt;br&gt;
&amp;gt; toc&lt;br&gt;
&amp;gt; Elapsed time is 0.447241 seconds.&lt;br&gt;
&amp;gt; Elapsed time is 0.556416 seconds.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I need to insert/delete a row into a large matrix.&lt;br&gt;
&amp;gt; How to make it fast?&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
One fast way of doing this is to first fill the rows with&lt;br&gt;
zeros, and then delete them using the 'any' function as follows:&lt;br&gt;
&lt;br&gt;
tic&lt;br&gt;
x = rand(50000,10);&lt;br&gt;
for i = 50000:-100:1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;x(i,:) = [];&lt;br&gt;
end&lt;br&gt;
toc&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt;Elapsed time is 4.813505 seconds.&lt;br&gt;
&lt;br&gt;
tic&lt;br&gt;
x = rand(50000,10);&lt;br&gt;
for i = 50000:-100:1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;x(i,:) = 0;&lt;br&gt;
end&lt;br&gt;
x(~any(x,2),:) = [];&lt;br&gt;
toc&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt;Elapsed time is 0.058070 seconds.&lt;br&gt;
&lt;br&gt;
I cannot give a technical explanation for why the second&lt;br&gt;
method is so much faster, but I would like to hear one if&lt;br&gt;
anyone knows... I guess it has to do with the logical&lt;br&gt;
indexing. My own problem was to delete very many irregularly&lt;br&gt;
spaced rows from a matrix of app. the dimensions above, and&lt;br&gt;
this method does the job very quickly.</description>
    </item>
    <item>
      <pubDate>Tue, 22 Jul 2008 13:21:44 -0400</pubDate>
      <title>Re: how to insert/delete rows in a matrix without copy the rest of matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153352#444587</link>
      <author>Peter Boettcher</author>
      <description>&quot;Jens Christiansen&quot; &amp;lt;jenschristiansen@gmail.com&amp;gt; writes:&lt;br&gt;
&lt;br&gt;
&amp;gt; &quot;Chenyang &quot; &amp;lt;john.doe.nospam@mathworks.com&amp;gt; wrote in message&lt;br&gt;
&amp;gt; &amp;lt;f87kg3$9ri$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt;&amp;gt; Matlab is very slow add/delete a row in a matrix.&lt;br&gt;
&amp;gt;&amp;gt; It always trying to copy the whole matrix&lt;br&gt;
&amp;gt;&amp;gt; e.g., &lt;br&gt;
&amp;gt;&amp;gt; &lt;br&gt;
&amp;gt;&amp;gt; a = rand(5000);&lt;br&gt;
&amp;gt;&amp;gt; tic&lt;br&gt;
&amp;gt;&amp;gt; a(:,1) = [];&lt;br&gt;
&amp;gt;&amp;gt; toc&lt;br&gt;
&amp;gt;&amp;gt; tic&lt;br&gt;
&amp;gt;&amp;gt; a = [a(:,1),a];&lt;br&gt;
&amp;gt;&amp;gt; toc&lt;br&gt;
&amp;gt;&amp;gt; Elapsed time is 0.447241 seconds.&lt;br&gt;
&amp;gt;&amp;gt; Elapsed time is 0.556416 seconds.&lt;br&gt;
&amp;gt;&amp;gt; &lt;br&gt;
&amp;gt;&amp;gt; I need to insert/delete a row into a large matrix.&lt;br&gt;
&amp;gt;&amp;gt; How to make it fast?&lt;br&gt;
&amp;gt;&amp;gt; &lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; One fast way of doing this is to first fill the rows with&lt;br&gt;
&amp;gt; zeros, and then delete them using the 'any' function as follows:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; x = rand(50000,10);&lt;br&gt;
&amp;gt; for i = 50000:-100:1&lt;br&gt;
&amp;gt;     x(i,:) = [];&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; toc&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt;&amp;gt;Elapsed time is 4.813505 seconds.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; x = rand(50000,10);&lt;br&gt;
&amp;gt; for i = 50000:-100:1&lt;br&gt;
&amp;gt;     x(i,:) = 0;&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; x(~any(x,2),:) = [];&lt;br&gt;
&amp;gt; toc&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt;&amp;gt;Elapsed time is 0.058070 seconds.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I cannot give a technical explanation for why the second&lt;br&gt;
&amp;gt; method is so much faster, but I would like to hear one if&lt;br&gt;
&amp;gt; anyone knows... I guess it has to do with the logical&lt;br&gt;
&amp;gt; indexing. My own problem was to delete very many irregularly&lt;br&gt;
&amp;gt; spaced rows from a matrix of app. the dimensions above, and&lt;br&gt;
&amp;gt; this method does the job very quickly.&lt;br&gt;
&lt;br&gt;
I believe it has to do with memory reallocation / copying.  Think of&lt;br&gt;
deleting rows much like adding rows... if you don't preallocate, each&lt;br&gt;
time you add elements, MATLAB is forced to reallocate memory to the new&lt;br&gt;
larger size, and copy all the data over.&lt;br&gt;
&lt;br&gt;
Your trick is like &quot;preallocation&quot; for deleting.  In the loop you only&lt;br&gt;
compute and save (in some manner) the rows to be deleted.  Then the&lt;br&gt;
actual delete happens at once, which means only a single reallocation.&lt;br&gt;
&lt;br&gt;
My guess is that in this case, a &quot;vectorized&quot; delete would be even&lt;br&gt;
faster.  You really shouldn't time &quot;rand&quot;, as that isn't part of the&lt;br&gt;
delete process.&lt;br&gt;
&lt;br&gt;
x1 = rand(50000,10);&lt;br&gt;
x2 = rand(50000,10);&lt;br&gt;
&lt;br&gt;
tic&lt;br&gt;
for i = 50000:-100:1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;x1(i,:) = 0;&lt;br&gt;
end&lt;br&gt;
x1(~any(x1,2),:) = [];&lt;br&gt;
toc&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
tic&lt;br&gt;
rows = 50000:-100:1;&lt;br&gt;
x2(rows,:) = [];&lt;br&gt;
toc&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Elapsed time is 0.009575 seconds.&lt;br&gt;
Elapsed time is 0.006921 seconds.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
-Peter</description>
    </item>
    <item>
      <pubDate>Tue, 14 Jun 2011 19:04:04 -0400</pubDate>
      <title>Re: how to insert/delete rows in a matrix without copy the rest of matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153352#841125</link>
      <author>Jesse Sipple</author>
      <description>You can just create a condition vector, then use that condition vector to remove the rows of your matrix&lt;br&gt;
&lt;br&gt;
test=rand(500,3);&lt;br&gt;
test(49,:)=0;&lt;br&gt;
test(200,:)=0;&lt;br&gt;
&lt;br&gt;
condition=test(:,1)==0;&lt;br&gt;
test(condition,:)=[];&lt;br&gt;
&lt;br&gt;
size(test)</description>
    </item>
    <item>
      <pubDate>Wed, 07 Sep 2011 19:52:28 -0400</pubDate>
      <title>Re: how to insert/delete rows in a matrix without copy the rest of matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153352#851651</link>
      <author>Henrique </author>
      <description>&lt;br&gt;
tim davis;&lt;br&gt;
&lt;br&gt;
you may be able to help me out.  a 2007 posting of yours below is somewhat related to an issue i am having.  if you have time, please see here;&lt;br&gt;
&lt;br&gt;
&lt;a href=&quot;http://www.mathworks.com/matlabcentral/answers/15316-joining-matrices-while-combining-columns&quot;&gt;http://www.mathworks.com/matlabcentral/answers/15316-joining-matrices-while-combining-columns&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
thanks in advance.&lt;br&gt;
&lt;br&gt;
kiko&lt;br&gt;
&lt;br&gt;
&quot;Tim Davis&quot; &amp;lt;davis@cise.ufl.edu&amp;gt; wrote in message &amp;lt;f87uil$ggh$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Chenyang &quot; &amp;lt;john.doe.nospam@mathworks.com&amp;gt; wrote in message &amp;lt;f87t7h$mje$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Thank you, but this is not a &quot;true&quot; solution.  The codes are just for illustration of the problem.  What if you want to delete/add the 99th row?  &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; The problem lies Matlab copying whole matrix while actually only a very small portion of the matrix being modified.&lt;br&gt;
&amp;gt; &amp;gt; I guess to really solve this problem we have to find a data-structure that allows a matrix to span multiple memory segments and are somehow linked together by indexing pointers. &lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; MATLAB stores a dense matrix in an m-by-n array, in column major form, without pointers.  Using pointers would cause a dreadful slowdown in any matrix computations other than&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; a (:,1) = [ ] ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; which is why no pointers are used (the BLAS do not use pointers).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; So the problem is intrinsically difficult.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Your best solution would be to avoid the need to delete rows/columns at all.  Can you set the row or column to delete to zero, for example?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Another alternative would be to keep a list of rows and columns that have not been &quot;deleted&quot;, but not to change a itself.  Try this:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; a = rand (5000) ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; delrow = 99 ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; b = [a(1:delrow-1,:) ; a(delrow+1:end,:)] ;&lt;br&gt;
&amp;gt; toc&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; a (delrow,:) = [ ];&lt;br&gt;
&amp;gt; toc&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; [m n] = size (a)&lt;br&gt;
&amp;gt; p = logical (ones (m, 1)) ;&lt;br&gt;
&amp;gt; q = logical (ones (1, n)) ;&lt;br&gt;
&amp;gt; nrows = m ;&lt;br&gt;
&amp;gt; ncols = n ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % to delete a row in a:&lt;br&gt;
&amp;gt; p (delrow) = 0 ;&lt;br&gt;
&amp;gt; nrows = nrows - 1 ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % to delete a colum in a:&lt;br&gt;
&amp;gt; q (101) = 0 ;&lt;br&gt;
&amp;gt; ncols = ncols - 1 ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; b = a(p,q) ;&lt;br&gt;
&amp;gt; toc&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % try using a(p,q) explicitly:&lt;br&gt;
&amp;gt; x = rand (ncols,1) ;&lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; y = a(p,q) * x ;&lt;br&gt;
&amp;gt; toc&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; tic&lt;br&gt;
&amp;gt; y = b*x ;&lt;br&gt;
&amp;gt; toc&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The logical indexing is a little faster.</description>
    </item>
    <item>
      <pubDate>Wed, 07 Sep 2011 20:33:29 -0400</pubDate>
      <title>Re: how to insert/delete rows in a matrix without copy the rest of matrix</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153352#851654</link>
      <author>Bruno Luong</author>
      <description>&quot;Henrique &quot; &amp;lt;hademelo@mtu.edu&amp;gt; wrote in message &amp;lt;j48i1s$1t6$1@newscl01ah.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; tim davis;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; you may be able to help me out.  a 2007 posting of yours below is somewhat related to an issue i am having.  if you have time, please see here;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;a href=&quot;http://www.mathworks.com/matlabcentral/answers/15316-joining-matrices-while-combining-columns&quot;&gt;http://www.mathworks.com/matlabcentral/answers/15316-joining-matrices-while-combining-columns&lt;/a&gt;&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
I'm no Tim, and exceptionally I'll answer to a question that is asked somewhere else (I think it's better to replicate the question here for the convenience).&lt;br&gt;
&lt;br&gt;
% Data&lt;br&gt;
A=1+zeros(6)&lt;br&gt;
B=2+zeros(6)&lt;br&gt;
C=3+zeros(6)&lt;br&gt;
D=4+zeros(6)&lt;br&gt;
E=5+zeros(6)&lt;br&gt;
F=6+zeros(6)&lt;br&gt;
ABC = cat(3,A,B,C)&lt;br&gt;
DEF = cat(3,D,E,F)&lt;br&gt;
% joint&lt;br&gt;
%  [A D;&lt;br&gt;
%   B E;&lt;br&gt;
%   C F];&lt;br&gt;
% That's how we want the input to be:&lt;br&gt;
I = cat(4,ABC,DEF)&lt;br&gt;
&lt;br&gt;
% Engine, working on I&lt;br&gt;
[m n p q] = size(I);&lt;br&gt;
[R C] = ndgrid(1:m,1:n);&lt;br&gt;
shift_r = (0:p-1)*4; % 4 is non ovelapping size &amp;lt; m&lt;br&gt;
shift_c = (0:q-1)*4; % idem&lt;br&gt;
&lt;br&gt;
R = bsxfun(@plus, R, reshape(shift_r, 1,1,[],1));&lt;br&gt;
C = bsxfun(@plus, C, reshape(shift_c, 1,1,1,[]));&lt;br&gt;
R = repmat(R, [1 1 1 q]);&lt;br&gt;
C = repmat(C, [1 1 p 1]);&lt;br&gt;
JOINT = accumarray([R(:) C(:)], I(:))./ accumarray([R(:) C(:)], 1)</description>
    </item>
  </channel>
</rss>

