Info

此问题已关闭。 请重新打开它进行编辑或回答。

could anyone help me to solve the following issue.

1 次查看(过去 30 天)
code:
A=partitions(4,3)
total_number_of_partitions = length(A)
idx=randperm(total_number_of_partitions,1)
iwant= A(idx)
partdisp(iwant)
The code executes and gives me the result.
when i run the code then command line which gives idx needs to be changed every time.
In my main code i will running the above code more than one time.
while running the code more than one time sometimes idx remains the same.
Actually the idx number needs to be changed everytime when i execute the code.
so i need to have the code in such a way the idx needs to be checked first,if it remains to be the same it should be changed and then displayed and if it is different then it can be displayed.
could anyone please help me on this.
  3 个评论
Walter Roberson
Walter Roberson 2019-5-13
Dennis's approach is a good one if you want to use each possible value exactly once.
However, the question in the form written only forbids using the same value as the immmediately after, and does not forbid (for example) the sequence 7, 3, 7, 3, 7, 3, ... since that satisfies that it is changing each time.
Dennis
Dennis 2019-5-13
Good point Walter. Here is an alternative approach:
A=1:5;
idx=randi(size(A,2),1,1000);
idx=idx(find(diff(idx)));
for i=1:numel(idx)
disp(A(idx(i)))
end

回答(0 个)

此问题已关闭。

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by