필터 지우기
필터 지우기

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 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