Quantcast
Viewing latest article 8
Browse Latest Browse All 36

Re: Bit of help with arrays: Uni Homework.

"Carsci" wrote in message <nu4r4o$q9a$1@newscl01ah.mathworks.com>...
> "Anthony Keane" wrote in message <nu3slj$kdp$1@newscl01ah.mathworks.com>...
> > Hey guys, I have been scratching my head for ages over this, I obviously don't want you to do my homework for me as I need to learn. But I have spent 17 hours on it...
> > Many many variations made.
> > Here's the question, and I will give you the code I have so far, and the problem I am having:
> >
> > The Question:
> > ---------------------------------------------------------------------------------------------------------------------
> > Vector of Body mass index (BMI) [1 Mark]
> > BMI is defined by the formula: BMI = w/ h2, where w is the subject’s weight (in kg) and h is height (in metre). According to the World Health Organization (WHO), a person can be in one of the following states:
> > - underweight, if his/her BMI is smaller than 18.5 (kg/m2)
> > - normal weight, if BMI is not less than 18.5 and no more than 25
> > - overweight, if 25<= BMI < 30
> > - obesity, if BMI is no less than 30.
> >
> > Write a script file and save it with name MyFirstName_bmi_vect.m (replace MyFirstName with your own first name), to calculate the BMI and determine the weight state of a set of persons.
> > 1) Create two variables (Weight and Height) to store a set of 100 values for weight in kg and height in meter, respectively. Use randi function to generate the values, e.g. randi([30, 80], 1, 100) generates 1 by 100 random integers in [30, 80]; randi([100,200], 1,100)/ 100 can generate 100 random numbers in [1.0, 2.0].
> > 2) Calculate BMI value for the persons according to the formula, and store the values to variable BMI.
> > 3) Plot the BMI values.
> > 4) Use for loop and conditional statements to determine the BMI state of these 100 persons with regard to their BMI and WHO definition, and store their BMI states into a new vector StateBMI of size 1x100. The values stored in the vector StateBMI are set to one of the four values: 1, 2, 3 and 4, which correspond to states of underweight, normal weight, overweight and obesity, respectively.
> >
> > Run the script, test it, demonstrate it when ready, and record your program results.
> > --------------------------------------------------------------------------------------------------------------------
> >
> > My better code so far:
> > --------------------------------------------------------------------------------------------------------------------
> >
> > disp('Welcome to the BMI Calucluator')
> > W = randi([30,80],1,100);
> > h = randi([100,200], 1,100)/ 100;
> > BMI=W/h.^2;
> >
> > for i=1:100;
> > if (BMI<18.5);
> > disp(BMI);
> > elseif (BMI>18.5)&&(BMI<25);
> > disp(BMI);
> > elseif (BMI>=25)&&(BMI<=30);
> > disp(BMI);
> > elseif (BMI>30);
> > disp(BMI);
> > end
> >
> > end
> >
> > StateBMI=zeros(1,4);
> > A=('Underweight');
> > B=('NormalWeight');
> > C=('Overweight');
> > D=('Obese');
> > ----------------------------------------------------------------------------------------------------------------
> >
> > Now the problem I am having is that it only calculates the BMI once and displays the same value 100 times in my code.
> > My question is, how do I make it run the equation new each time for the 100 iterations and then store it into a 1*100 vector with 4 values?
> > I can figure out the rest as soon as I can get to that point.
> > Thank you in advance for your help, I have until Wednesday night to finish it (and one more, but the next question involves a similar problem).
>
> Hi. BMI doesn't have 100 values because it is not being calculated as a vector as you need to vectorize both the division and power i.e. BMI=W/h.^2; is missing a period.
>
> Also you should declare StateBMI=zeros(1,100); before the for loop say at the top of the code otherwise Matlab will have to resize the array 100 times.
>
> Also some of your if elseif conditions are redundant. Try this as a partial solution for you to complete
> for i=1:100;
> if (BMI(i)<18.5);
> elseif (BMI(i)<25);
> StateBMI(i)=2;
> elseif (BMI(i)>=25)&&(BMI(i)<=30);
> else StateBMI(i)=4;
> end
> end
> disp(StateBMI);

------------------------------------------------------------------------------------------------------------------
Hi, I used your bit of code there, and I see where you are going with the elseif statements.

I don't quite get what you mean by vectoring the function though?
As
W = randi([30,80],1,100);
h = randi([100,200], 1,100)/ 100;
Are already 2 vectors, should the formula be under BMI?
Here is my latest iteration.

%Anthony Andrew Keane's BMI Code Week 3
disp('Welcome to the BMI Calucluator')
StateBMI=zeros(1,100);
W = randi([30,80],1,100);
h = randi([100,200], 1,100)/ 100;
BMI =([W,h.^2]),1,100);
A=('Underweight');
B=('NormalWeight');
C=('Overweight');
D=('Obese');

 for i=1:100;
     if (BMI(i)<18.5);
         StateBMI(i)=1;
     elseif (BMI(i)<25);
        StateBMI(i)=2;
     elseif (BMI(i)>=25)&&(BMI(i)<=30);
        StateBMI(i)=3;
     elseif (BMI(i)>30);
         StateBMI(i)=4;
     end
 end
 if StateBMI==1;
    disp(A)
 elseif StateBMI==2;
     disp(B)
 elseif StateBMI==3;
     disp(C)
 elseif StateBMI==4;
     disp(D)
 end

*Obviously I am getting errors now*

Viewing latest article 8
Browse Latest Browse All 36

Trending Articles