how do i print only the last answer in a for loop

3 次查看(过去 30 天)
function c = newton_interpolation(x,y)
[p,n]=size(x);
c = zeros(n, n);
c(:, 1)= y';
for j=2:n
for k=1:(n-j+1)
c(k,j) = (c(k+1, j-1) - c(k,j-1))/(x(k+j-1)-x(k))
end
end
when i print this i get every itteration of the answer but i only need the last
  1 个评论
VBBV
VBBV 2023-6-27
function c = newton_interpolation(x,y)
[p,n]=size(x);
c = zeros(n, n);
c(:, 1)= y';
for j=2:n
for k=1:(n-j+1)
c(k,j) = (c(k+1, j-1) - c(k,j-1))/(x(k+j-1)-x(k)); % put a semicolon here
end
end
c(end,end) %// use the end index for matrix
end
use end index for the c matrix

请先登录,再进行评论。

回答(1 个)

madhan ravi
madhan ravi 2020-11-14
c(k,j) = (c(k+1, j-1) - c(k,j-1))/(x(k+j-1)-x(k)); %<- semicolon

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by