[英]Array manipulation with matlab / octave using circshift
I'm trying to produce an output that will take the last value of an array, and start the next array with the next lowest value found in that array. 我试图产生一个输出,该输出将采用数组的最后一个值,并从该数组中找到的下一个最小值开始下一个数组。 If there is no next lowest value I would like it to just end the loop See example of the answer I'm trying to get below. 如果没有下一个最小值,我希望它结束循环。请参见下面尝试获取的答案示例。
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
The code I'm using below only gets the first two rows correct and does something strange at the end any ideas how to fix this. 我在下面使用的代码只能正确纠正前两行,并且在最后提出任何奇怪的解决方法。
Code: 码:
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
The results I get are 我得到的结果是
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
As you can see the last row should read 如您所见,最后一行应为
nxt_low_idx_val = 2
3.0000 7.0100 9.0000 11.0000 5.0000 7.0000
Any ideas how to fix this? 任何想法如何解决这一问题?
Thanks 谢谢
Too lazy to look at your code. 懒得看您的代码。 How about this? 这个怎么样?
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
I Just needed to move some things under the for loop 我只需要在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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.