错误:sub2ind:使用Octave 4.2.2时,所有下标的大小必须相同

时间:2019-06-17 04:23:27

标签: arrays octave

我遇到一个错误:sub2ind:所有下标必须具有相同的大小。似乎存在问题的行是idx = sub2ind ([n m], r, c)

如果我尝试将数组设置为特定大小,则会收到该错误(请参阅下面的某些导致问题的值)

%values that cause the error
array_rows=3;  %3,3,7,7
array_cols=6;  %6,5,3,5

如何使其与这些值以及其他导致此错误的值一起使用(我确定还有更多导致此错误的值)?

原始问题已由Andy here

回答并解决。

请参见下面的代码:

    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) > 1)
        r = [r n-v+1:-1:v+1];
      endif
    endfunction

    function idx = ring (n, m , v)

      if (2*(v-1) > 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

Values that Work with the Code run on TIO

Values that don't work with the Code run on TIO

Ps:我正在使用Ubuntu 18.04 Octave 4.2.2

0 个答案:

没有答案