I need to innovatively produce a 140x8 matrix filled with random grades from 140 students
조회 수: 1 (최근 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!