title: error: sub2ind: all subscripts must be of the same size using Octave 4.2.2
body: <p>I'm getting an <strong>error: sub2ind: all subscripts must be of the same size.</strong>  The line that seems to be the issue is <code>idx = sub2ind ([n m], r, c)</code>  </p>
  
  <p>If I try and make the array certain sizes I get that error (see some of the values below that cause the issue)</p>
  
  <pre><code>%values that cause the error
  array_rows=3;  %3,3,7,7
  array_cols=6;  %6,5,3,5
  </code></pre>
  
  <p>How can I get it to work with these values and others that cause this error (I'm sure there are more values that cause this error)?</p>
  
  <p><strong>The Original question:</strong> was answered and solved by <a href="https://stackoverflow.com/questions/56573666/alternating-and-shifting-sections-of-an-array/56580908#56580908">Andy here</a></p>
  
  <p>See code below:</p>
  
  <pre><code>    function r = rndtrip (n, m, v)
        rv = @(x) x - 2 * (v - 1);
        r = [v * ones(1,rv(m)-1) v:n-v+1 (n-v+1)*ones(1,rv(m)-2)];
        if (rv(m) &gt; 1)
          r = [r n-v+1:-1:v+1];
        endif
      endfunction
  
      function idx = ring (n, m , v)
  
        if (2*(v-1) &gt; min (n, m))
          r = [];
        else
          r = rndtrip (n, m, v);
          c = circshift (rndtrip (m, n, v)(:), - n + 2 * v - 1).';
          idx = sub2ind ([n m], r, c)
  
        endif
  
      endfunction
  
      # your array
      array_rows=3;  %3,3,7,7
      array_cols=6;  %6,5,3,5
  
      I_orig = reshape (1:array_rows*array_cols, array_cols, array_rows).' %reshape array %I_orig = reshape (1:30, 5, 6).' 
      [rw_orig col_orig]=size(I_orig);
  
      I_new=I_orig; %where the new rotated values will be stored
  
      min_rows_cols=min(rw_orig,col_orig); %get min number of row - columns used to calc number of rings
  
      r=(-1).^(1:ceil(min_rows_cols/2)) %  (toggles beween -1 1)
      %r = [1 -1] %used to have ring rotate CCW or CW can be done manually
  
      for k = 1:numel(r)
        idx = ring (rows(I_new), columns(I_new), k);
        I_new(idx) = I_new(circshift(idx(:), r(k)));
      endfor
  
      I_new
  
  
  
  
  
  I_orig =
  
          1    2    3    4    5    6
          7    8    9   10   11   12
         13   14   15   16   17   18
  
  r =
  
        -1   1
  idx =
  
          1    4    7   10   13   16   17   18   15   12    9    6    3    2
  error: sub2ind: all subscripts must be of the same size
  error: called from ring at line 16 column 9
  </code></pre>
  
  <p><a href="https://tio.run/##bVPbbuIwEH3PVxxpFa1NTXcDgkpBVCv1ab9gHyq2CokBi8Su7Fxgf54d20BB7YvjuZ05Mz4xZVv08nTadLpsldGwWMLqqrXqHUwLNAI9TwDbU@AXO3AcMMYEI7CeLhlf@CDFXnvyGS0dy4TtWcPHGUef63H/kBGS//DRXXzCV75YbcCCA88ER44znkUoysdZTp@QKnWlNgmdF7pJciWuqoOnrvQ28kYgfsafjFjv@TyjUTrG@U2rCF47eXV9WsEihEoKlcqWbqc2LdG@ZFGK9lks54K2ovEQVhQ39Pg9FkeGrltPlK7AXjWalYAVKAPPONz9dN9wNJ1FYW1xTML5Zs3glrMFkE7FVDyJp7O/NLVbzr1/LmYUmSXJ7zdj1dZPI92ueJdgWf4BMvqoE/h89ylEHemlOLiRfgU6/SkwE5j7/OTVDjGF0MJltXTqn2SxkBZJvLQcltFeIB120kq0Owlyw5q2aGWFvqg76TCousZawrXGyipJ6P0CtTgvWezcTlzacYLcyja8tO6atbQwG4Id6DEop2u0Q@eoQ2tQFnV5m0TqcUlil4zE8viXJiulqtldzx8TzpHCWNykTTnAWrPd1kR5LQcpNcaZl3MaBJaRtUJ6abujfy5KNU6Ll5c/HpHOstB@3Ir@FDRFV9T1kUROsT3hZDlxlTWzXru3gvfsWFgr6e885NXeB/EGi1ERp6poXJXs3UG6lu05p3QvQmPPL3U6/Qc" rel="nofollow noreferrer">Values that Work with the Code run on TIO</a></p>
  
  <p><a href="https://tio.run/##bVPbbuIwEH3PVxxpFa1NTXcDgkpBVCv1ab9gHyq2CokBi8Su7Fxgf54d20BB7YvjuZ05Mz4xZVv08nTadLpsldGwWMLqqrXqHUwLNAI9TwDbU@AXO3AcMMYEI7CeLhlf@CDFXnvyGS0dy4TtWcPHGUef63H/kBGS//DRXXzCV75YbcCCA88ER44znkUoysdZTp@QKnWlNgmdF7pJciWuqoOnrvQ28kYgfsafjFjv@TyjUTrG@U2rCF47eXV9WsEihEoKlcqWbqc2LdG@ZFGK9lks54K2ovEQVhQ39Pg9FkeGrltPlK7AXjWalYAVKAPPONz9dN9wNJ1FYW1xTML5Zs3gltMFkE7FVDyJp7O/NLVbzr1/LmYUmSXJ7zdj1dZPI92ueJdgWf4BMvqoE/h89ylEHemlOLiRfgU6/SkwE5j7/OTVDjGF0MJltXTqn2SxkBZJvLQcltFeIB120kq0Owlyw5q2aGWFvqg76TCousZawrXGyipJ6P0CtTgvWezcTlzacYLcyja8tO6atbQwG4Id6DEop2u0Q@eoQ2tQFnV5m0TqcUlil4zE8viXJiulqtldzx8TzpHCWNykTTnAWrPd1kR5LQcpNcaZl3MaBJaRtUJ6abujfy5KNU6Ll5c/HpHOstB@3Ir@FDRFV9T1kUROsT3hZDlxlTWzXru3gvfsWFgr6e885NXeB/EGi1ERp6poXJXs3UG6lu05p3QvQmPPL3U6/Qc" rel="nofollow noreferrer">Values that don't work with the Code run on TIO</a></p>
  
  <p><strong>Ps: I'm using Ubuntu 18.04 Octave 4.2.2</strong></p>