How to access multiple elements of an array efficienty in C++?
<p>This if my first post, I hope I'll meet the standards...</p>

<p>I'm translating into <strong>c++</strong> (at which I'm quite new) a program originally written in <strong>MATLAB</strong> for reasons of efficiency. The piece of code I am actually working on resumes accesses to various indexes of a vector (matrix) in one step. For example, if M1 is a matrix of size, let's say, 10x15, the program would define a new one as follows:</p>

<pre><code>idxs1 = [1 2 3];
idxs2 = [1 2 3 4 5];
M2 = M1 (idxs1 , idxs2);
</code></pre>

<p>resulting M2 as a matrix of size 3x5. Now, I guess what <strong>MATLAB</strong> actually does is access one by one the various places of M1 given by the indexes and then construct M2 by rearranging the many contents acquired, all very efficiently.</p>

<p>My question is, how can I reproduce such mechanism in <strong>c++</strong>? As far as I know there is no direct way to access in a row various indexes of an array, and the <code>for</code> loop I'm using seems rather cumbersome. Maybe there's some intelligent way to do it without demanding 'too much' processor time? Also, for the sake of educational purposes, I would be grateful if someone could explain what MATLAB actually does when such operation is performed.</p>

<p>Thanks in advance and sorry for the eventual inconveniences!</p>

<p>P.S: Just in case it adds anything to the question, I'm working with <strong>MEX</strong> files to link both languages.
P.S2: By the way, I found some related questions but regarding other languages:</p>

<ul>
<li><p>python: <a href="https://stackoverflow.com/questions/28005531/access-multiple-elements-of-an-array">Access multiple elements of an array</a></p></li>
<li><p>perl: <a href="https://stackoverflow.com/questions/32590623/how-can-i-use-an-array-slice-to-access-several-elements-of-an-array-simultaneous">How can I use an array slice to access several elements of an array simultaneously?</a></p></li>
<li><p>c: <a href="https://stackoverflow.com/questions/26330127/how-to-access-multiple-elements-of-array-in-one-go">How to access multiple elements of array in one go?</a></p></li>
</ul>