代码之家  ›  专栏  ›  技术社区  ›  Rick T

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

  •  0
  • Rick T  · 技术社区  · 7 年前

    我得到了一份工作 错误: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:我使用的是Ubuntu18.04八度音阶4.2.2

    0 回复  |  直到 7 年前