Info

This question is closed. Reopen it to edit or answer.

could anyone help me to solve the following issue.

1 view (last 30 days)
jaah navi
jaah navi on 13 May 2019
Closed: MATLAB Answer Bot on 20 Aug 2021
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 Comments
Walter Roberson
Walter Roberson on 13 May 2019
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 on 13 May 2019
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

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!