title: How do I splice two vectors in MATLAB?
body: <p>I need to splice two vectors based on a condition that also takes a vector as an argument. Example:</p>
  <pre class="lang-matlab prettyprint-override"><code>vec_cond = -5:5;       % The exact values are calculated differently
  vec1     = 0:10;
  vec2     = 5:15;
  </code></pre>
  <p>I need a resulting vector to be comprised from values out of both vectors based on a condition from the third vector. Let's assume this is the condition: <code>vec_cond &gt;= 0</code></p>
  <p>Then if this is true, I want <code>vec_result</code> to have values from <code>vec1</code> on appropriate indexes, and if not, take values from <code>vec2</code> on appropriate indexes:</p>
  <pre class="lang-py prettyprint-override"><code>vec_result = vec1 if (vec_cond &gt;=0) else vec2
  </code></pre>
  <p>This is portion of my MATLAB script (original comments were Czech) where I would need to use that:</p>
  <pre class="lang-matlab prettyprint-override"><code>%% Draw output current and voltage characteristics
  R       = 100:5:2*10^3;             % Load rezistor          [ohm]
  U_2     = R .* (I * 10^(-3));       % Load voltage             [V]
  U_1stab = U_LM + U_x + U_2;         % Min. required input voltage
                                      % for stabilization        [V]
  U_delta = U_1 - U_1stab;            % Difference between actual and
                                      % min. req. input voltage  [V]
  U_2norm = U_1 - U_LM - U_x          % Calculating output load
                                      % voltage based on params  [V]
  
  I_z     = U_2norm ./ R .* 10^3;     % Load current param based[mA]
  I_r1    = I * I_z.^0;               % Stabilizator current    [mA]
  </code></pre>
  <p>So the condition would be <code>U_delta &gt;= 0</code>.</p>
  <p>I tried to use a ternary operator, which I found <a href="https://www.mathworks.com/matlabcentral/cody/problems/44243-ternary-conditional-operator" rel="nofollow noreferrer">here</a>:</p>
  <pre class="lang-matlab prettyprint-override"><code>I_graph = (U_delta &gt;= 0) : (@() I) : (@() I_z);         % Current splice  [mA]
  U_graph = (U_delta &gt;= 0) : (@() U_2) : (@() U_2norm);   % Voltage splice   [V]
  </code></pre>
  <p>That means that for <code>I_graph</code>, if the condition is met, take a constant value <code>I</code> and vectorize it, otherwise take values from <code>I_z</code> vector. For <code>U_graph</code>, if the condition is met, take values from <code>U_2</code> vector, otherwise take constant value of <code>U_2norm</code> and vectorize it.</p>
  <p>But it didn't work, this is what it tells me:</p>
  <pre><code>Operator ':' is not supported for operands of type 'function_handle'.
  
  Error in vypocet1 (line 52)
  I_graph = (U_delta &gt;= 0) : (@() I) : (@() I_z);         % Current splice  [mA]
  </code></pre>
  <p>I guess that I might want to use <code>for</code> loop, but I'm not sure how it will help me and how can I actually construct the necessary vector using a <code>for</code> loop.</p>