Info

# I would like to evaluate a function for N=11,21,51,and 101 using a for loop, but I can only get it to do the last N.

1 view (last 30 days)
Corey Katz on 8 Feb 2018
Closed: MATLAB Answer Bot on 20 Aug 2021
I tried to loop over N and get four vectors out of the function. N is the number of points and the function evaluates an estimate for the derivatives at those points. So if there are N=11 points it will give me a vector of 11 derivative estimates. I need to do this for N=11,21,51,and 101 so i was trying to loop over these N's to do this at the same time. Unfortunately, it only gives the estimates for the last N and I cannot figure out how to store the other estimates as the loop goes along. I know I can just write four separate lines of could and get these values but I was trying to condense the code. My code below only gives the N=101 estimate. Is there any way of doing this and store the information as it goes.
for N= [11,21,51,101]
df=fowarddif(N);
end
I tried many things, like if statements, but that ends up being more code than just the four lines. Any help would be greatly appreciated.

Les Beckham on 8 Feb 2018
You will need to use an array to store the four different df results. Since your df is going to be different sizes depending on the input argument N, I would recommend a cell array since normal arrays can't have different sized elements (e.g., rows).
Try this:
N = [11 21 51 101];
for (idx = 1:length(N))
df{idx} = fowarddif(N(idx));
end
This will store the four different results separately in the cell array df.
To extract the results for N=11, for example, you would use df{1}.
I hope this helps.