Azzera filtri
Azzera filtri

How to include this interpolation in a "for" loop?

7 visualizzazioni (ultimi 30 giorni)
Hi,
I would like to include this interpolation in a "for" loop. I have a number of sets of data and I would like to make a "for" loop for their interpolation. Can anyone please help me in making the loop?
x1 = [1 4 8 10];
y1 = [1 2 6 7];
z1 = [2 3 5 9];
x2 = [2 6 8 9]
y2 = [5 9 7 6]
z2 = [3 4 7 1]
.
.
.
xn = [ ]
yn = [ ]
zn = [ ]
w1 = interp1(x1,y1,5,'linear')
plot(x1,y1,'-',5,w,'*')
w2 = interp1(x2,y2,5,'linear')
plot(x2,y2,'-',5,w,'*')
.
.
.
wn = interp1(xn,yn,5,'linear')
plot(xn,yn,'-',5,w,'*')

Risposta accettata

OCDER
OCDER il 17 Ott 2017
First, you have to rename all your variables. Labeling your variables x1,x2,x3 ...xn makes it very difficult to use a for loop. This is a fairly common problem that could be fixed using cell arrays. Read:
If you have 1000's of these variables... here's a solution that fixes that:
Best is to start by storing data in cells (use the find and replace option to fix x1 to x{1}, etc:
x{1} =
y{1} =
z{1} =
x{2} =
y{2} =
z{2} =
Then you can use for loops!
w = cell(size(x));
for k = 1:length(x)
w{k} = interp1(x{k},y{k},5,'linear')
plot(x{k},y{k},'-',5,w{k},'*')
if k == 1 %In case you want to plot over many times
hold on
end
end
  3 Commenti
Stephen23
Stephen23 il 31 Ott 2017
You could also use cellfun:
>> X = {[1,4,8,10],[2,6,8,9]};
>> Y = {[1,2,6, 7],[5,9,7,6]};
>> fun = @(x,y)interp1(x,y,5,'linear');
>> C = cellfun(fun,X,Y,'uni',0)
this makes it simple to put all of the output values into one array and then plot it.

Accedi per commentare.

Più risposte (0)

Categorie

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

Community Treasure Hunt

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

Start Hunting!

Translated by