Use the rand function to create a 3 times 4 matrix of uniformly distributed rand
ID: 3109996 • Letter: U
Question
Use the rand function to create a 3 times 4 matrix of uniformly distributed random numbers. Call the matrix M. Consult the help files if necessary on how to use the rand function. Create a second matrix P, the same size as M. For each element in M that is greater than 0.5, put a "1" in the corresponding position in P. For each element in M that is less than 0.5, put a "2" in the corresponding position in P. If the element in M is exactly equal to 0.5, put a "0" in the corresponding position in P.Explanation / Answer
clc;
clear all;
close all;
M=rand(3,4)
[m,n]=size(M);
for i=1:m
for j=1:n
if M(i,j)<0.5
C(i,j)=2;
elseif M(i,j)>0.5
C(i,j)=1;
else
C(i,j)=0;
end
end
end
C
OUTPUT:
M =
0.7094 0.6797 0.1190 0.3404
0.7547 0.6551 0.4984 0.5853
0.2760 0.1626 0.9597 0.2238
C =
1 1 2 2
1 1 2 1
2 2 1 2
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.