I need to innovatively produce a 140x8 matrix filled with random grades from 140 students
1 view (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
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]
Lm = StuGra < 50 % Logical Matrix
StuGra(Lm) = randi([50 100], 1, nnz(Lm))
.
2 Comments
Star Strider
on 2 Sep 2024
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
More Answers (1)
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.
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!