<p>Using the <code>zip</code> function, Python allows for loops to traverse multiple sequences in parallel.  </p>

<p><code>for (x,y) in zip(List1, List2):</code></p>

<p>Does MATLAB have an equivalent syntax? If not, what is the best way to iterate over two parallel arrays at the same time using MATLAB?</p>

[{'answer_id': 49514, 'author': 'sven', 'author_id': 46, 'author_profile': 'https://Stackoverflow.com/users/46', 'pm_score': 3, 'selected': False, 'text': '<p>If I\'m not mistaken the zip function you use in python <a href="http://groups.google.com/group/comp.lang.python/browse_thread/thread/195e57735ff6ee56" rel="noreferrer">creates a pair of the items found in list1 and list2</a>. Basically it still is a for loop with the addition that it will retrieve the data from the two seperate lists for you, instead that you have to do it yourself.</p>\n\n<p>So maybe your best option is to use a <em>standard</em> for loop like this:</p>\n\n<pre><code>for i=1:length(a)\n  c(i) = a(i) + b(i);\nend\n</code></pre>\n\n<p>or whatever you have to do with the data.</p>\n\n<p>If you really are talking about parallel computing then you should take a look at the <a href="http://www.mathworks.com/products/parallel-computing/" rel="noreferrer">Parallel Computing Toolbox</a> for matlab, and more specifically at <a href="http://www.mathworks.com/access/helpdesk/help/toolbox/distcomp/index.html?/access/helpdesk/help/toolbox/distcomp/brb2x2l-1.html" rel="noreferrer">parfor</a></p>\n'}, {'answer_id': 51137, 'author': 'DMC', 'author_id': 3148, 'author_profile': 'https://Stackoverflow.com/users/3148', 'pm_score': 3, 'selected': False, 'text': '<p>Tested only in octave... (no matlab license). Variations of arrayfun() exist, check the documentation.</p>\n\n<pre><code>dostuff = @(my_ten, my_one) my_ten + my_one;\n\ntens = [ 10 20 30 ];\nones = [ 1 2 3];\n\nx = arrayfun(dostuff, tens, ones);\n\nx\n</code></pre>\n\n<p>Yields...</p>\n\n<pre><code>x =\n\n   11   22   33\n</code></pre>\n'}, {'answer_id': 65903, 'author': 'mattiast', 'author_id': 8272, 'author_profile': 'https://Stackoverflow.com/users/8272', 'pm_score': 4, 'selected': False, 'text': '<p>If x and y are column vectors, you can do:</p>\n\n<pre><code>for i=[x\';y\']\n# do stuff with i(1) and i(2)\nend\n</code></pre>\n\n<p>(with row vectors, just use <code>x</code> and <code>y</code>).</p>\n\n<p>Here is an example run:</p>\n\n<pre class="lang-matlab prettyprint-override"><code>&gt;&gt; x=[1 ; 2; 3;]\n\nx =\n\n     1\n     2\n     3\n\n&gt;&gt; y=[10 ; 20; 30;]\n\ny =\n\n    10\n    20\n    30\n\n&gt;&gt; for i=[x\';y\']\ndisp([\'size of i = \' num2str(size(i)) \', i(1) = \' num2str(i(1)) \', i(2) = \' num2str(i(2))])\nend\nsize of i = 2  1, i(1) = 1, i(2) = 10\nsize of i = 2  1, i(1) = 2, i(2) = 20\nsize of i = 2  1, i(1) = 3, i(2) = 30\n&gt;&gt; \n</code></pre>\n'}, {'answer_id': 138886, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': -1, 'selected': False, 'text': '<p><code>for</code> loops in MATLAB used to be slow, but this is not true anymore.</p>\n\n<p>So vectorizing is not always the miracle solution. Just use the profiler, and <code>tic</code> and <code>toc</code> functions to help you identify possible bottlenecks.</p>\n'}, {'answer_id': 218618, 'author': 'bastibe', 'author_id': 1034, 'author_profile': 'https://Stackoverflow.com/users/1034', 'pm_score': 2, 'selected': False, 'text': "<p>I would recommend to join the two arrays for the computation:</p>\n\n<pre><code>% assuming you have column vectors a and b\nx = [a b];\n\nfor i = 1:length(a)\n    % do stuff with one row...\n    x(i,:);\nend\n</code></pre>\n\n<p>This will work great if your functions can work with vectors. Then again, many functions can even work with matrices, so you wouldn't even need the loop.</p>\n"}, {'answer_id': 54482864, 'author': 'Sean', 'author_id': 2194422, 'author_profile': 'https://Stackoverflow.com/users/2194422', 'pm_score': 0, 'selected': False, 'text': "<pre><code>for (x,y) in zip(List1, List2):\n</code></pre>\n\n<p>should be for example:</p>\n\n<pre><code>&gt;&gt; for row = {'string' 10\n&gt;&gt;           'property' 100 }'\n&gt;&gt;    fprintf([row{1,:} '%d\\n'], row{2, :});\n&gt;&gt; end\nstring10\nproperty100\n</code></pre>\n\n<p>This is tricky because the cell is more than 2x2, and the cell is even transposed. Please try this.</p>\n\n<p>And this is another example:</p>\n\n<pre><code>&gt;&gt; cStr = cell(1,10);cStr(:)={'string'};\n&gt;&gt; cNum=cell(1,10);for cnt=1:10, cNum(cnt)={cnt};\n&gt;&gt; for row = {cStr{:}; cNum{:}}\n&gt;&gt;    fprintf([row{1,:} '%d\\n'], row{2,:});\n&gt;&gt; end\nstring1\nstring2\nstring3\nstring4\nstring5\nstring6\nstring7\nstring8\nstring9\nstring10\n</code></pre>\n"}, {'answer_id': 64389879, 'author': 'Alex', 'author_id': 11554957, 'author_profile': 'https://Stackoverflow.com/users/11554957', 'pm_score': 0, 'selected': False, 'text': '<p>If I have two arrays al and bl with same dimension No 2 size and I want to iterate through this dimension (say multiply <code>al(i)*bl(:,i)</code>). Then the following code will do:</p>\n<pre class="lang-matlab prettyprint-override"><code>al = 1:9;\nbl = [11:19; 21:29];\n\nfor data = [num2cell(al); num2cell(bl,1)]\n\n\xa0\xa0\xa0 [a, b] = data{:};\n\xa0\xa0\xa0 disp(a*b)\n\nend\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/49307', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5148/']