× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Inserting One Row Each Time in a Sequence from Matrix into Another Matrix After Every nth Row in Matlab

Ray Phan
Jun 17, 2015
<p>For canonical purposes, here's how you'd do it via loops. This is a bit inefficient since you're mutating the array at each iteration, but it's really simple to read. Given that your two matrices are stored in <code>A</code> (100 x 3) and <code>B</code> (10 x 3), you would do:</p> <pre><code>out = []; for idx = 1 : 10 out = [out; A((idx-1)*10 + 1 : 10*idx,:); B(idx,:)]; end </code></pre> <p>At each iteration, we pick out 10 rows of <code>A</code> and 1 row of <code>B</code> and we concatenate these 11 rows onto <code>out</code>. This happens 10 times, resulting in 330 rows with 3 columns.</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/30856087/Inserting%20One%20Row%20Each%20Time%20in%20a%20Sequence%20from%20Matrix%20into%20Another%20Matrix%20After%20Every%20nth%20Row%20in%20Matlab/30856684">Stack Overflow</a>.</p>
comments powered by Disqus