saving values in a for loop

1 visualización (últimos 30 días)
soloby
soloby el 25 de Jun. de 2015
Comentada: Walter Roberson el 25 de Jun. de 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?

Respuesta aceptada

Walter Roberson
Walter Roberson el 25 de Jun. de 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 comentarios
soloby
soloby el 25 de Jun. de 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 el 25 de Jun. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by