For loop for a function

I have the following function and I want to run it for age 30 to 100. I think I need to develop a for loop here. Can anyone please help me out? I know if I change the value of age step by step like 30, 31 .... then I can do it but I need to run it as a for loop
function hf = myfun(age)
age = [30:100]
hf = zeros(5,5);
hf(1,2) = exp(-0.0625.*age-0.0134); % exp(age effect+time effect)
hf(1,5) = exp(-9.65573+0.01844+0.08218*age+0.02246); % exp(intercept+ age effect+time effect)
hf(2,3) = exp(-1.6660-0.1116.*age-0.0025); % exp(intercept+ age effect+time effect)
hf(2,4) = exp(-8.96236+0.07691.*age + 0.00978); % assuming the death rate of male of same age(Hubener et al.)
hf(2,5) = exp(-9.65573+0.08218.*age+0.02246); % self-mortality
hf(3,2) = exp(-0.0625.*age-0.0134+0.0676); %exp(intercept+ age effect+time effect+marriage once before)
hf(3,5) = exp(-9.65573+0.08218.*age+0.02246-0.11853);
hf(4,2) = exp(-0.4176-0.0625-0.0134.*age);
hf(4,5) = exp(-9.65573+0.08218.*age+0.02246-0.00415);
hf(1,1) = -(hf(1,2)+hf(1,5))
hf(2,2) = -(hf(2,3)+hf(2,4)+hf(2,5))
hf(3,3) = -(hf(3,2)+hf(3,5))
hf(4,4) = -(hf(4,2)+hf(4,5))
end

 Accepted Answer

You construct the for loop like this:
for i=1:size(age,2) %by referencing your age array, you can change the age values and it will still work
hf(i,:,:) = zeros(5,5);
hf(i,1,2) = exp(-0.0625.*age(i)-0.0134); %index every step of hf to rely on i, and use the i'th value of age for the evaluations of the whole way
end
In the end, you will have hf(1,:,:) contain your results from age(1) in this case age=30, hf(2,:,:) age = 31 and so on.

6 Comments

This is how I tried it, but it did not work. Sorry I have never used loops for running functions.
function hf = myfun(age)
age= [25:30]
for i=1:size(age)
hf(i,:,:) = zeros(5,5);
hf(i,1,2) = 1-exp(-exp(-0.0625.*age-0.0134)); % exp(age effect+time effect)
hf(i,1,5) = 1-exp(-exp(-9.65573+0.01844+0.08218*age+0.02246)); % exp(intercept+ age effect+time effect)
hf(i,2,3) = 1-exp(-exp(-1.6660-0.1116.*age-0.0025)); % exp(intercept+ age effect+time effect)
hf(i,2,4) = 1-exp(-exp(-8.96236+0.07691.*age + 0.00978)); % assuming the death rate of male of same age(Hubener et al.)
hf(i,2,5) = 1-exp(-exp(-9.65573+0.08218.*age+0.02246)); % self-mortality
hf(i,3,2) = 1-exp(-exp(-0.0625.*age-0.0134+0.0676)); %exp(intercept+ age effect+time effect+marriage once before)
hf(i,3,5) = 1-exp(-exp(-9.65573+0.08218.*age+0.02246-0.11853));
hf(i,4,2) = 1-exp(-exp(-0.4716-0.0625-0.0134.*age));
hf(i,4,5) = 1-exp(-exp(-9.65573+0.08218.*age+0.02246-0.00415));
hf(i,1,1) = 1-(hf(1,2)+hf(1,5))
hf(i,2,2) = 1-(hf(2,3)+hf(2,4)+hf(2,5))
hf(i,3,3) = 1-(hf(3,2)+hf(3,5))
hf(i,4,4) = 1-(hf(4,2)+hf(4,5))
hf(i,5,5) = 1
end
end
You need to call age(i) in every instance where you simply call age now. Also, every hf should be indexed, so
hf(i,3,3) = 1-(hf(i,3,2)+hf(i,3,5)) %like this
Oh sorry I indexed each hf with i now. This is the code now but it gives error. Can you just run my code and tell me how would you do it for age 30 to 35? It will be easier to understand.
Thank you so so much
function hf = myfun(age)
age= 25:30
for i=1:size(age,2)
hf(i,:,:) = zeros(5,5);
hf(i,1,2) = 1-exp(-exp(-0.0625.*age-0.0134)); % exp(age effect+time effect)
hf(i,1,5) = 1-exp(-exp(-9.65573+0.01844+0.08218*age+0.02246)); % exp(intercept+ age effect+time effect)
hf(i,2,3) = 1-exp(-exp(-1.6660-0.1116.*age-0.0025)); % exp(intercept+ age effect+time effect)
hf(i,2,4) = 1-exp(-exp(-8.96236+0.07691.*age + 0.00978)); % assuming the death rate of male of same age(Hubener et al.)
hf(i,2,5) = 1-exp(-exp(-9.65573+0.08218.*age+0.02246)); % self-mortality
hf(i,3,2) = 1-exp(-exp(-0.0625.*age-0.0134+0.0676)); %exp(intercept+ age effect+time effect+marriage once before)
hf(i,3,5) = 1-exp(-exp(-9.65573+0.08218.*age+0.02246-0.11853));
hf(i,4,2) = 1-exp(-exp(-0.4716-0.0625-0.0134.*age));
hf(i,4,5) = 1-exp(-exp(-9.65573+0.08218.*age+0.02246-0.00415));
hf(i,1,1) = 1-(hf(i,1,2)+hf(i,1,5))
hf(i,2,2) = 1-(hf(i,2,3)+hf(i,2,4)+hf(i,2,5))
hf(i,3,3) = 1-(hf(i,3,2)+hf(i,3,5))
hf(i,4,4) = 1-(hf(i,4,2)+hf(i,4,5))
hf(i,5,5) = 1
end
end
You still need to index age.
hf(i,1,2) = 1-exp(-exp(-0.0625.*age(i)-0.0134)); %index age the whole way through
Yes I did and the code runs now but I want a separate hf for each age like a matrix, hf for age 30 and so on. In this case my output is correct but not in the manner I want
function hf = myfun(age)
age= 30:40
for i=1:size(age,2)
hf(i,:,:) = zeros(5,5);
hf(i,1,2) = 1-exp(-exp(-0.0625.*age(i)-0.0134)); % exp(age effect+time effect)
hf(i,1,5) = 1-exp(-exp(-9.65573+0.01844+0.08218*age(i)+0.02246)); % exp(intercept+ age effect+time effect)
hf(i,2,3) = 1-exp(-exp(-1.6660-0.1116.*age(i)-0.0025)); % exp(intercept+ age effect+time effect)
hf(i,2,4) = 1-exp(-exp(-8.96236+0.07691.*age(i) + 0.00978)); % assuming the death rate of male of same age(Hubener et al.)
hf(i,2,5) = 1-exp(-exp(-9.65573+0.08218.*age(i)+0.02246)); % self-mortality
hf(i,3,2) = 1-exp(-exp(-0.0625.*age(i)-0.0134+0.0676)); %exp(intercept+ age effect+time effect+marriage once before)
hf(i,3,5) = 1-exp(-exp(-9.65573+0.08218.*age(i)+0.02246-0.11853));
hf(i,4,2) = 1-exp(-exp(-0.4716-0.0625-0.0134.*age(i)));
hf(i,4,5) = 1-exp(-exp(-9.65573+0.08218.*age(i)+0.02246-0.00415));
hf(i,1,1) = 1-(hf(i,1,2)+hf(i,1,5))
hf(i,2,2) = 1-(hf(i,2,3)+hf(i,2,4)+hf(i,2,5))
hf(i,3,3) = 1-(hf(i,3,2)+hf(i,3,5))
hf(i,4,4) = 1-(hf(i,4,2)+hf(i,4,5))
hf(i,5,5) = 1
end
end
instead of getting hf(1,:,:) I am getting hf(:,:,1), hf(:,:,2) and so on.
Is there any syntax problem?

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 15 Feb 2021

Commented:

on 15 Feb 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!