# saving values in a for loop

1 view (last 30 days)
soloby on 25 Jun 2015
Commented: Walter Roberson on 25 Jun 2015
My code looks something like this
x = -10:0.1:10;
f1 = trapmf(x,[-2 0 0 2]);
for k = 0.05:0.05:1
index = find(abs(f1-k)<10^(-10))
Xidx = x(index)
end
I want to save my Xidx to plot x(index) for all k values, but it currently saves only the last iteration
Any solution?

Walter Roberson on 25 Jun 2015
x = -10:0.1:10;
f1 = trapmf(x,[-2 0 0 2]);
kvals = 0.05:0.05:1;
for kidx = 1 : length(kvals)
k = kvals(kidx);
index = find(abs(f1-k)<10^(-10))
Xidx(kidx) = x(index)
end
soloby on 25 Jun 2015
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in fdsasfd (line 9) Xidx(kidx) = x(index)
Walter Roberson on 25 Jun 2015
x = -10:0.1:10;
f1 = trapmf(x,[-2 0 0 2]);
kvals = 0.05:0.05:1;
for kidx = 1 : length(kvals)
k = kvals(kidx);
index = find(abs(f1-k)<10^(-10))
Xidx{kidx} = x(index)
end
but be cautioned that your cell arrays might come up empty and might come up as different sizes.