While Loops and Criteria

6 views (last 30 days)
Callum
Callum on 29 Apr 2024
Commented: Voss on 30 Apr 2024
Hi - I'm not sure if I've provided enough information for this but I'll try. I have 4 matrices: easy_matrix, medium_matrix, hard_matrix, expert_matrix. Within these matrices are 9 rows of 1x4 arrays of different topics.(like motion_easy or waves_medium, etc.). All of the 1x4 arrays have specific, but different numbers in them (they don't have identifiers though). If possible, I'd like the final_unshuffled matrix to return in such a way where if, say, easy_matrix pulls motion_easy, none of the others can pull the motion_topic (i.e. medium_matrix can't pull motion_medium). I was thinking of making easy_matrix the reference row so that the other matrices (medium, hard and expert) all have a reference point for a while loop but I couldn't make it work. Any help is greatly appreciated! Thank you in advance :)
easy_matrix = [motion_easy; waves_easy; quantum_easy; radiation_easy; electricity_easy; materials_easy; ef_easy; mf_easy; thermal_easy];
rand_row_easy = easy_matrix(randi(4),:); %Random pull from easy
disp(rand_row_easy)
medium_matrix = [motion_medium; waves_medium; quantum_medium; radiation_medium; electricity_medium; materials_medium; ef_medium; mf_medium; thermal_medium];
rand_row_medium = medium_matrix(randi(4),:); %Random pull from medium
disp(rand_row_medium)
hard_matrix = [motion_hard; waves_hard; quantum_hard; radiation_hard; electricity_hard; materials_hard; ef_hard; mf_hard; thermal_hard];
rand_row_hard = hard_matrix(randi(4),:); %Random pull from hard
disp(rand_row_hard)
expert_matrix = [motion_expert; waves_expert; quantum_expert; radiation_expert; electricity_expert; materials_expert; ef_expert; mf_expert; thermal_expert];
rand_row_expert = expert_matrix(randi(4),:); %Random pull from expert
disp(rand_row_expert)
%You could say, if the rand_row_easy is motion_easy, it
%medium_matrix, hard_matrix, and expert_matrix can't be from the
%motion array.
%LOCATION STAMP
disp('break_1')
fprintf('\n')
disp('Therefore, combining the random pulls:')
%Final, un-shuffled, Matrix:
final_unshuffled = [rand_row_easy; rand_row_medium; rand_row_hard; rand_row_expert];

Accepted Answer

Voss
Voss on 29 Apr 2024
I'm not sure why it's always randi(4), when the matrices all have 9 rows. Are you constrained to pulling from only the first 4 rows for some reason?
Regardless, here's an easy way to generate 4 distinct random integers, which you can then use to pull a row from each matrix:
idx = randperm(9,4);
idx will be a 1x4 vector of random integers between 1 and 9 inclusive, without repeats.
If you do indeed need to constrain them to be between 1 and 4 inclusive, then use:
idx = randperm(4,4);
Then pulling a row from each matrix to construct final_unshuffled can be done like this:
final_unshuffled = [easy_matrix(idx(1),:); medium_matrix(idx(2),:); hard_matrix(idx(3),:); expert_matrix(idx(4),:)];
  5 Comments
Callum
Callum on 30 Apr 2024
Hey Voss,
Sorry, only just getting round to doing this. I can't believe it was that simple, thanks for the help!
Voss
Voss on 30 Apr 2024
"Does this also create the condition that if, e.g. motion_easy was chosen for the easy row, then motion_medium can’t be chosen for the medium row, nor motion_hard for the hard row, etc. for expert?"
Yes, because the random integers are guaranteed the be distinct.
Any other questions, please let me know, and if this answer worked, please "Accept" it. Thanks!

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!