Pseudorandom sequence with NO MORE than two consecutive numbers
이전 댓글 표시
Hi everyone.
I want to create an array of numbers from 1 to 4 with a pseudorandom order. A column has 100 rows, and each number must be five times in a subgroup of 20 rows. Additionally, the same number appears in two consecutive rows at the most.
The solution that I found here ( https://es.mathworks.com/matlabcentral/answers/743412-how-to-randomize-trials-without-presenting-the-same-trial-consecutively ), has the main problem that in a subset of 4 rows you can predict which number is the 4th.
Avoiding a while would be unbelievable so that my desired final matrix will be 100x10
Here I drop a short example what I need:
Correct pseudorandomization:
2 3 4 3 3 1 2 4 1 3 3 1 2 4 2 1 1 4 2 4...
Incorrect:
4 3 1 2 4 1 3 2 2 4 3 1 4 3 2 1 2 3 4 1...
any help will be welcome.
댓글 수: 2
Dyuman Joshi
2024년 1월 12일
"Additionally, the same number appears in two consecutive rows at the most."
Do you mean column instead of rows?
Pablo
2024년 1월 12일
답변 (3개)
% Create the matrix
randomMatrix = createRandomMatrix();
% Display the first 20 rows of the matrix
disp(randomMatrix(1:20,:))
function matrix = createRandomMatrix()
numRows = 100;
numColumns = 10;
numGroups = 5;
values = 1:4;
% Preallocate the matrix
matrix = zeros(numRows, numColumns);
for col = 1:numColumns
% Repeat for each subgroup of rows
for group = 1:numGroups
isValid = false;
while ~isValid
% Generate a random permutation of the numbers for this subgroup
subgroup = repmat(values, 1, numRows/(numGroups*length(values)));
subgroup = subgroup(randperm(length(subgroup)));
% Check the no more than two consecutive numbers condition
for i = 3:length(subgroup)
if subgroup(i) == subgroup(i-1) && subgroup(i) == subgroup(i-2)
break;
end
end
% If we reached the end without breaking, the subgroup is valid
if i == length(subgroup)
isValid = true;
end
end
% Fill the subgroup into the matrix
matrix((group-1)*numRows/numGroups+1:group*numRows/numGroups, col) = subgroup;
end
end
end
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
@Pablo Check this out.
% Create the matrix
randomMatrix = createRandomMatrix();
% Display the first 20 rows of the matrix
disp(randomMatrix(1:20,:))
function matrix = createRandomMatrix()
numRows = 100;
numColumns = 10;
numGroups = 5;
values = 1:4;
% Preallocate the matrix
matrix = zeros(numRows, numColumns);
for col = 1:numColumns
for group = 1:numGroups
isValid = false;
while ~isValid
% Generate a random permutation of the numbers for this subgroup
subgroupSize = numRows/(numGroups*length(values));
subgroup = repmat(values, 1, subgroupSize);
subgroup = subgroup(randperm(length(subgroup)));
% Check that no more than two consecutive numbers are the same
isValid = true;
for i = 3:length(subgroup)
if subgroup(i) == subgroup(i-1) && subgroup(i) == subgroup(i-2)
isValid = false;
break;
end
end
end
% Fill the subgroup into the matrix
startRow = (group-1)*numRows/numGroups + 1;
endRow = group*numRows/numGroups;
matrix(startRow:endRow, col) = subgroup;
end
end
end
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
@Pablo Check this out.
% Create and display the matrix
randomMatrix = createRandomMatrix();
disp(randomMatrix(1:40,:));
function randomMatrix = createRandomMatrix()
numRows = 100;
numColumns = 10;
values = 1:4;
% Preallocate the matrix
randomMatrix = zeros(numRows, numColumns);
for col = 1:numColumns
% Initialize the column
col_data = [];
while length(col_data) < numRows
% Shuffle the values and add them to the column
shuffledValues = values(randperm(length(values)));
col_data = [col_data, shuffledValues];
% Check if the last three elements are the same, if so, remove them
if length(col_data) > 2 && col_data(end) == col_data(end-1) && col_data(end) == col_data(end-2)
col_data(end-2:end) = [];
end
end
% Assign the generated column data to the matrix
randomMatrix(:, col) = col_data(1:numRows).';
end
end
-----------------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
카테고리
도움말 센터 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!