フィルターのクリア

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

7 ビュー (過去 30 日間)
Ismail Qeshta
Ismail Qeshta 2017 年 10 月 17 日
コメント済み: Ismail Qeshta 2017 年 11 月 1 日
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,'*')

採用された回答

OCDER
OCDER 2017 年 10 月 17 日
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 件のコメント
Stephen23
Stephen23 2017 年 10 月 31 日
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.
Ismail Qeshta
Ismail Qeshta 2017 年 11 月 1 日
Thanks Stephen

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by