Clear Filters
Clear Filters

For loop for a function

3 views (last 30 days)
susman on 15 Feb 2021
Commented: susman on 15 Feb 2021
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))

Accepted Answer

Jakob B. Nielsen
Jakob B. Nielsen on 15 Feb 2021
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
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.
susman on 15 Feb 2021
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
susman on 15 Feb 2021
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)


Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!