Nested For Loop Statement: Write the following scripts using nested for loops ma
ID: 3815247 • Letter: N
Question
Nested For Loop Statement: Write the following scripts using nested for loops mat - 5 10 7 7 9 9 6 2 3 9 1 5 2 10 8 6 5 1 7 1 1 6 1 9 9 a) Display the elements of the given matrix, mat. It should look as above. b) Display only upper triangular elements (row index column index) of the given matrix, mat. 4.Non-vectorization: This problem is the same as Problem 1 in Homework 3, but you need to use for and if statements to solve this problem (non-vectorized code to practice for and if statements). The data of height, weight, and age of a group of people were collected and stored in the vector as below (You can access the ith person's data in the following way. height (i), weight (i), age (i)). Using the data, solve the following problems using the logical indexing in Matlab. height = (8.9 6.8 7.4 4.9 5.5 5.7 7.1 5.7 6.7 3.9 5.6 5.2 4.4 6.5 6.3 6.0 4.7 7.1 6.4 5.7); weight = (144 133 128 201 183 156 125 133 146 166 123 103 121 157 158 159 147 154 14C 167); age = [60 25 33 37 54 27 56 25 53 45 59 56 65 65 37 55 30 22 57 45); a) Calculate the mean values of age for someone whose height is greater than (>) 6.0, AND weight is less than () 6.0. AND weight is greater than (>) 150. AND age is less than (Explanation / Answer
3.a)
a=[5 9 1 6 1;10 6 5 5 6;7 2 2 1 1;7 3 10 7 9;9 9 8 1 9];
[h,w,~]=size(a);
for i=1:h
for j=1:w
a(i,j); // prints matrix a
end
end
a
3.b)
for i=1:h
for j=1:w
if(i<j)
b(i,j)=a(i,j); // upper triangular matix
end
end
end
b
3.c)
sum=0;
for i=1:h
for j=1:w
if(i>j)
sum=sum+a(i,j); // calculates sum of lower triangle
end
end
end
sum
Output
>> matrix
a =
5 9 1 6 1
10 6 5 5 6
7 2 2 1 1
7 3 10 7 9
9 9 8 1 9
b =
0 9 1 6 1
0 0 5 5 6
0 0 0 1 1
0 0 0 0 9
sum =
66
4.a)
height=[8.9 6.8 7.4 4.9 5.5 5.7 7.1 5.7 6.7 3.9 5.6 5.2 4.4 6.5 6.3 6.0 4.7 7.1 6.4 5.7];
weight=[144 133 128 201 183 156 125 133 146 166 123 103 121 157 158 159 147 154 140 167];
age=[60 25 33 37 54 27 56 25 53 45 59 56 65 65 37 55 30 22 57 45];
sum=0;
avg=0;
for i=1:20
if(height(i)>6.0 && weight(i)<150)
b=age(i); // stores the ages according to the condition in b
end
end
[r h ~]=size(b);
for i=1:h
sum=sum+b(i);
end
avg=sum/r; // finds the mean
avg
4.b)
count=0;
for i=1:20
if(height(i)>6.0 && weight(i)>150 && age(i)<50)
count=count+1; // calculates the number of persons meeting the condition
end
end
count
output
>> bmi
avg =
57
count =
2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.