Azzera filtri
Azzera filtri

saving values in a for loop

2 visualizzazioni (ultimi 30 giorni)
soloby
soloby il 25 Giu 2015
Commentato: Walter Roberson il 25 Giu 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?

Risposta accettata

Walter Roberson
Walter Roberson il 25 Giu 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 Commenti
soloby
soloby il 25 Giu 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 il 25 Giu 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by