MATLAB: Traversing an Array Write your own script that will find the minimum ele
ID: 672167 • Letter: M
Question
MATLAB: Traversing an Array
Write your own script that will find the minimum element of an array. You CANNOT use the build-in function, min(). Your script will start with generating a 20 by 20 array consisting of random integers in the range [-100,100]. If will then loop through all element and find the min value of all. This is 4 based on the . In addition to finding the smallest number, this program will also count the occurrences of the smallest number. You cannot use build-in functions. You are going to traverse the array and count This is another variation of . Instead of finding the smallest number, this program will look for the two smallest numbers. (They can be of the same value). You cannot use build-in functions. You are going to traverse the array and look for the two mins.Explanation / Answer
r = randi([-100 100],20,20)
minm=r(1,1)
for i=1:20
for j=1:20
if r(i,j)<minm
minm=r(i,j)
end
end
end
count=0
for i=1:20
for j=1:20
if r(i,j)==minm
count=count+1
end
end
end
secondM=r(1,1)
for i=1:20
for j=1:20
if r(i,j)<secondM && r(i,j)!=minm
secondM=r(i,j)
end
end
end
fprintf(' Minimum number= %d ',minm)
fprintf('Number of minimum numbers= %d ',count)
fprintf('Second Minimum= %d ',secondM)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.