Clear Filters
Clear Filters

I need to innovatively produce a 140x8 matrix filled with random grades from 140 students

2 views (last 30 days)
Code so far:
%StuGra=randi(100,140,8);
A=(50:1:100);
StuGra=[60 70 80 90;50 40 30 20;20 20 20 90;10 90 10 90]; %BOLD
if StuGra(StuGra < 50)
x=A(randi(length(A),1));
StuGra(StuGra < 50)= x;
end
disp(StuGra)
I understand the top line is basically all i need for the code but im told to make it innovative, so im trying to replace all values within the random matric that are below 50, with another set of random values between 50 and 100. Ive tried a few different things and this is the best ive gotten, however it replaces all values below 50 with a single value that is now above, making them all 1 score, which i dont really want, could anyone give me some pointers to what i could try? The 3rd line is just a test matrix to see what im doing.

Accepted Answer

Star Strider
Star Strider on 1 Sep 2024
OPne approach is to use logiical indexing —
StuGra=[60 70 80 90;50 40 30 20;20 20 20 90;10 90 10 90]
StuGra = 4x4
60 70 80 90 50 40 30 20 20 20 20 90 10 90 10 90
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Lm = StuGra < 50 % Logical Matrix
Lm = 4x4 logical array
0 0 0 0 0 1 1 1 1 1 1 0 1 0 1 0
StuGra(Lm) = randi([50 100], 1, nnz(Lm))
StuGra = 4x4
60 70 80 90 50 77 74 56 53 63 61 90 77 90 90 90
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
.

More Answers (1)

Image Analyst
Image Analyst on 1 Sep 2024
Not sure what qualifies as "innovative" but how about making a matrix of all prime numbers, rescaled?
% Get a list of 8*140 prime numbers.
p = primes(9011); % A vector 8*140 long
% Reshape into matrix and make into integers in the range 50-100
p2 = round(rescale(p(1:140*8), 50, 100));
% Scramble their order.
randomIndexes = randperm(140*8);
p3 = reshape(p2(randomIndexes), 140, []) % Final 8*140 matrix
Is that innovative enough? Otherwise describe what would be innovative enough.
This looks like a homework problem. If you have any questions ask your instructor or read the link below to get started:
Obviously, if it is homework, you're not allowed to turn in our code as your own, or you could get into trouble with your instructor.
  1 Comment
Nathan Begg
Nathan Begg on 2 Sep 2024
Edited: Nathan Begg on 2 Sep 2024
that would certainly work, ill try playing around with it to see if i could make my own spin on the idea! and oh yes it is for an assignment, so i wont be taking your code directly haha, just couldnt think of a way, with my currently knowledge, to problem solve around it

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!