saving values in a for loop

1 view (last 30 days)
soloby
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?

Accepted Answer

Walter Roberson
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
  2 Comments
soloby
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
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.

Sign in to comment.

More Answers (0)

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!