【问题标题】:Array manipulation with matlab / octave using circshift使用 circshift 使用 matlab / octave 进行数组操作
【发布时间】:2013-08-07 02:42:34
【问题描述】:

我正在尝试生成一个输出,该输出将采用数组的最后一个值,并以在该数组中找到的下一个最小值开始下一个数组。如果没有下一个最低值,我希望它结束​​循环请参阅下面我试图得到的答案示例。

9.0000   11.0000    5.0000    7.0000    3.0000    7.0100
7.0000    3.0000    7.0100    9.0000   11.0000    5.0000
3.0000    7.0100    9.0000   11.0000    5.0000    7.0000

我在下面使用的代码只使前两行正确,最后做了一些奇怪的事情,任何想法如何解决这个问题。

代码:

clc
a=[9,11,5,7,3,7.01];
[a_sorted, a_idx] = sort(a, 2); %sorts array along with getting index values of numbers
a_sorted=a_sorted'; % sort into col
a_idx=a_idx'; % sort into col
a_val_idx=[a_sorted a_idx]; % combine array

loop_amount=length(find(a<a(end))) %how many values are less than the last value, loop this many times

for yy=1:loop_amount

    a_val=find(a_val_idx(:,1)<a(end)); %find idx of next lowest value from end 
    nxt_low_idx_val=a_val_idx(a_val(end),2) %get idx of the next lowest value from end

    b=circshift(a,[0 (length(a)-nxt_low_idx_val+1)])

    a=b;

end

我得到的结果是

loop_amount =  3
a =
    9.0000   11.0000    5.0000    7.0000    3.0000    7.0100

nxt_low_idx_val =  4
a =
    7.0000    3.0000    7.0100    9.0000   11.0000    5.0000

nxt_low_idx_val =  5
a =
   11.0000    5.0000    7.0000    3.0000    7.0100    9.0000

nxt_low_idx_val =  6

如您所见,最后一行应为

nxt_low_idx_val =  2

3.0000    7.0100    9.0000   11.0000    5.0000    7.0000

任何想法如何解决这个问题?

谢谢

【问题讨论】:

    标签: arrays matlab sorting octave


    【解决方案1】:

    懒得看你的代码。这个怎么样?

    a = [9,11,5,7,3,7.01];
    disp(' ')
    disp(a) % display original value
    len = length(a);
    
    loop_count = sum(a<a(end)); % as per your code
    for count = 1:loop_count
      b = a(1:end-1); % copy of a, will be overwritten
      b(b>a(end)) = NaN; % these values do not count
      if(all(isnan(b)))
        break % exit if there are no lower values
      end
      [aux ind] = max(b); % max of the remaing values
      perm = mod(ind+(0:len-1),len); % cyclic shift
      perm(perm==0) = len; % correct zero to len
      a = a(perm); % do the shift
      disp(a) % display new value
    end
    

    【讨论】:

    • 如果没有下一个最低值我想结束循环
    • 这只是一遍又一遍地连续循环
    • 是的。因为您的数据永远不会达到退出条件(“没有下一个最低值”)。您想要固定数量的迭代吗?
    • 好的,我已经在你的代码中设置了最大迭代次数
    【解决方案2】:

    我只是需要在 for 循环下移动一些东西

    clc
    a=[9,11,5,7,3,7.01];
    
    
    loop_amount=length(find(a<a(end))) %how many values are less than the last value, loop this many times
    
    for yy=1:loop_amount
        [a_sorted, a_idx] = sort(a, 2); %sorts array along with getting index values of numbers
        a_sorted=a_sorted'; % sort into col
        a_idx=a_idx'; % sort into col
        a_val_idx=[a_sorted a_idx]; % combine array
        a_val=find(a_val_idx(:,1)<a(end)); %find idx of next lowest value from end 
        nxt_low_idx_val=a_val_idx(a_val(end),2) %get idx of the next lowest value from end
    
        b=circshift(a,[0 (length(a)-nxt_low_idx_val+1)])
    
        a=b;
    
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-05
      • 1970-01-01
      • 2014-10-29
      • 2016-12-09
      • 1970-01-01
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多