How to rearrange random binary input for each iteration?

I am having 7 randomnly generated binary input. With this input I have performed some set of mathematical calculations. Now i need to repeat the mathematical calculations with changed order of binary input and this has to be followed for each iteration until a certain condition is met.
Example:
A = [011; 101; 010; 001; 100; 110; 111]
Expected output: Input order of A has to be varied for each iteration.

Answers (1)

Have a look at the nextperm tool on the file exchange. It will allow you to cycle through all permutations.
HTH

9 Comments

This is not working. Can you suggest some other method.
This does not give a binary vector
A = [011; 101; 010; 001; 100; 110; 111]
A = 7×1
11 101 10 1 100 110 111
Those are decimal, base 10 numbers, not base 2 numbers.
Please attach your actual code.
I have used the following code to generate binary numbers and this is stored in a text file and it is called in main program.
A= randperm(7);
b= dec2bin(r);
Unrecognized function or variable 'r'.
c = convertCharsToStrings(b)
mat = reshape (b,[7,3]);
matstng=string(mat);
A = randperm(7)
A = 1×7
5 2 7 3 6 4 1
for col = (dec2bin(A) - '0').'
row = col.'
result = row(1)*11 + row(2).^2 - row(3) %some calculation
end
row = 1×3
1 0 1
result = 10
row = 1×3
0 1 0
result = 1
row = 1×3
1 1 1
result = 11
row = 1×3
0 1 1
result = 0
row = 1×3
1 1 0
result = 12
row = 1×3
1 0 0
result = 11
row = 1×3
0 0 1
result = -1
Actually this numbers has to vary for each iteration. Can you please suggest the mehod.
The number does vary for each iteration. Each row = output line shows the input that was received, and you can see that no two of the inputs were the same.
Actually i need to apply it for higher number of bits, this is my example problem.
result = row(1)*11 + row(2).^2 - row(3), this means if i need to generate 12 bit number, then how should i write this code.
Just write the expression using indexing.
If you have a linear expression, you could use multiplication such as [3 -1 5 11 4 2 -5 ...] * col
A = randi([0 2^12-1], 1, 5)
for col = (dec2bin(A) - '0').'
row = col.'
result = row(1)*11 + row(2).^2 - row(3) + 7*row(4).^2 - row(5).^9 + row(6)*2 - row(7)*5 + row(8).^3 - row(9)*15 + row(10)*3 + row(11)*8 - row(9)*4 %some calculation
end
row = 1×12
1 0 1 1 1 1 1 0 1 0 0 0
result = -6
row = 1×12
1 1 1 1 0 1 0 0 1 0 0 0
result = 1
row = 1×12
1 0 1 1 0 1 1 0 0 1 0 0
result = 17
row = 1×12
1 0 1 0 0 0 1 0 1 0 1 1
result = -6
row = 1×12
1 1 0 0 1 1 0 0 1 1 0 0
result = -3

Sign in to comment.

Categories

Products

Release

R2020a

Asked:

on 9 Jan 2023

Commented:

on 10 Jan 2023

Community Treasure Hunt

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

Start Hunting!