MATLAB CODE: function [density] = myCalculateDensity( X ) a. INPUTS: A 2D matrix
ID: 3694289 • Letter: M
Question
MATLAB CODE:
function [density] = myCalculateDensity( X )
a. INPUTS: A 2D matrix X. For this project, each element of X will represent a pixel of bone. The higher the number, the higher the density of that little square of bone.
b. FUNCTIONALITY: i. This function calculates the average density of all non-zero elements of a matrix X.
c. OUTPUTS: i. density
d. TEST CASE:
>> A = [176.2360 171.0640 115.8960 162.4440; 207.2680 179.6840 0.0000 0.0000; 233.1280 190.0280 145.2040 146.9280; 248.6440 177.9600 181.4080 164.1680];
>> myCalculateDensity(A)
ans =
178.5757
Explanation / Answer
function [density] = myCalculateDensity(X)
m = length(X);
n = size(X,1);
sum = 0.0;
count = 0.0;
for i = 1:m
for j = 1:n
if X(i,j) != 0
sum = sum + X(i,j);
count = count+1;
endif
end
endfor
density = double(sum) / double(count);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.