title: Advanced subscript assignment to avoid a for loop in MATLAB--many binary operations at once
body: <p>I've got a big array, say A, with values in {1,...,n}, and another array B of the same size.</p>
  
  <p>I want to get all of the following:</p>
  
  <pre><code>B(A==1)
  B(A==2)
  ...
  B(A==n)
  </code></pre>
  
  <p>and then do something else with the results (not so important for now).</p>
  
  <p>I tried things like:</p>
  
  <pre><code>[x,y] = B(A==[1:n])
  </code></pre>
  
  <p>and</p>
  
  <pre><code>[x,y] = [B(A==1), B(A==2), ..., B(A==n)]
  </code></pre>
  
  <p>Of course to no avail.</p>
  
  <p>The for loop approach</p>
  
  <pre><code>for ii=1:n
      dummy=B(A==1)
      other stuff
  end
  </code></pre>
  
  <p>works, but I'm convinced I can avoid for loops for everything in MATLAB! Stuck here, though. Any suggestions? Perhaps some sort of inline anonymous function call?</p>