How to shuffle numbers for each iteration?

I am having 'n' randomnly generated positive decimal number. With this input I have performed some set of mathematical calculations. Now i need to repeat the mathematical calculations with changed order of decimal number and this has to be followed for each iteration until a certain condition is met.
Example:
A= 5, 2, 4, 3, 7, 1, 6
Expected output: Shuffle decimal number for each iteration. [only positive number is required]

7 Kommentare

Jiri Hajek
Jiri Hajek am 12 Jan. 2023
Hi, your question is not very clear. Do you need to permute digits in a number?
Dyuman Joshi
Dyuman Joshi am 12 Jan. 2023
Do you need the numbers 1 to 7 in a (different) random order for each iteration?
ASHA PON
ASHA PON am 12 Jan. 2023
Yes. I need numbers 1 to 7 in different order for each iteration. Thank you
Try randperm -
Or if you want all permutations in once, and then loop through them, check out perms()
n=7;
iter=10;
for idx=1:iter
randperm(n)
end
ans = 1×7
6 7 2 3 1 4 5
ans = 1×7
3 7 5 1 6 2 4
ans = 1×7
2 5 6 7 4 3 1
ans = 1×7
1 6 5 2 3 7 4
ans = 1×7
7 4 2 5 6 1 3
ans = 1×7
4 3 7 5 6 2 1
ans = 1×7
1 5 7 2 3 4 6
ans = 1×7
1 5 7 4 3 2 6
ans = 1×7
6 4 7 1 2 5 3
ans = 1×7
7 6 2 1 5 4 3
ASHA PON
ASHA PON am 12 Jan. 2023
Thank you. This is what i expected
ASHA PON
ASHA PON am 19 Jan. 2023
Hello Dyuman joshi. The code you have suggested works very well, when it is applied seperately. But while i use this same code and apply some calculation after randperm(n), I didn't get different decimal numbers for each iteration. Can you please guide me where I am making mistake. Thank you.
Dyuman Joshi
Dyuman Joshi am 19 Jan. 2023
Can you show your code? Without the code, it is difficult to tell where the problem is.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Tobias
Tobias am 12 Jan. 2023

0 Stimmen

It seems you try to do something like a minmum search. Probably, you can solve the origin problem in a more target oriented way.
For your question to shuffle a vector you could do something like this:
Expect having a vector with several digits. In this example 1 to 7:
vector = 1:7;
For example, you could shuffle it like this:
vector = vector(randperm(length(vector)));

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2020b

Gefragt:

am 12 Jan. 2023

Kommentiert:

am 19 Jan. 2023

Community Treasure Hunt

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

Start Hunting!

Translated by