MATLAB Exercise 1. Create a 4X8 matrix of randomly generated numbers 2. Loop thr
ID: 2079215 • Letter: M
Question
MATLAB Exercise
1. Create a 4X8 matrix of randomly generated numbers
2. Loop through all rows and columns and test whether each element is greater than 0.5
3. Report results of the test along with the value of the matrix element and its row-column position
4. Make sure to add exceptions to print out 1st, 2nd, and 3rd i.e. not 2th or 3th
5. Put this assembled code into a separte function that you can call from the command line with two inouts, corresponding to the number of rows and columns of the matrix
Explanation / Answer
MATLAB code
clear all
close all
clc
A=rand([4,8])
[m,n] = size(A);
len=m*n;
for i=1:len
if A(i)>0.5
B(i)=i;
end
end
B()
Out put:
A =
Columns 1 through 6
0.5688 0.1622 0.1656 0.6892 0.2290 0.5383
0.4694 0.7943 0.6020 0.7482 0.9133 0.9961
0.0119 0.3112 0.2630 0.4505 0.1524 0.0782
0.3371 0.5285 0.6541 0.0838 0.8258 0.4427
Columns 7 through 8
0.1067 0.8173
0.9619 0.8687
0.0046 0.0844
0.7749 0.3998
ans =
Columns 1 through 10
1 0 0 0 0 6 0 8 0 10
Columns 11 through 20
0 12 13 14 0 0 0 18 0 20
Columns 21 through 30
21 22 0 0 0 26 0 28 29 30
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.