While Loops and Criteria

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

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

My bad, yeah it should've been randi(9). That being said, my goal is essentially that I don't want repeated "topics" in the final_unshuffled matrix.
So all of the arrays in easy_matrix, medium_matrix, hard_matrix, and expert_matrix, are 1x4's with preset numbers (like they're pulled from a larger matrix).
I was trying to get the final_unshuffled matrix to display strictly different topics. So for example, if the easy_matrix produced a row, and that row was called motion_easy, even if the row is just numbers, then medium_matrix, hard_matrix nor expert_matrix can produce motion_medium, motion_hard, or motion_expert respectively.
While the numbers don't have any specific name identifiers to show was group they're in, I was wondering if it was possible to do this with a sort of 'string' check? I'm not sure if that exists but becase all the array names are called things like motion_easy or quantum_easy, or waves_hard, radiation_hard, I thought it might be possible to run a while loop which checks for a string identifier, like: if easy_matrix produces motion_easy, the others (medium, hard and expert) can't produced an array with 'motion' in the name.
I'm not sure if this is possible, apologies for the lengthy response, I wasn't sure if my point was clear in the question.
Thanks!
Voss
Voss on 30 Apr 2024
Edited: Voss on 30 Apr 2024
I think I understand. Since the topics are in the same order in each matrix, the task is essentially to generate 4 distinct random integers between 1 and 9 (inclusive), and use those as the row indices to pull from each respective (easy, medium, etc.) matrix. Right? That's what my answer does. No id strings or anything else are necessary. Again, it only works if the topics are the same and in the same order in each matrix, but that seems to be the case.
Hey Voss,
Thanks for the fast response - yeah you’re right, the order does stay the same. 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?
Hey Voss,
Sorry, only just getting round to doing this. I can't believe it was that simple, thanks for the help!
"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

Asked:

on 29 Apr 2024

Commented:

on 30 Apr 2024

Community Treasure Hunt

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

Start Hunting!