saving values in a for loop

1 次查看(过去 30 天)
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
Walter Roberson 2015-6-25
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 个评论
soloby
soloby 2015-6-25
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 2015-6-25
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by