I need to innovatively produce a 140x8 matrix filled with random grades from 140 students
2 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Star Strider
2024 年 9 月 1 日
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 件のコメント
Star Strider
2024 年 9 月 2 日
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
その他の回答 (1 件)
Image Analyst
2024 年 9 月 1 日
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.
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!