How can I interpolate rows in an array with a loop?

1 回表示 (過去 30 日間)
Paola
Paola 2021 年 8 月 17 日
回答済み: Yazan 2021 年 8 月 18 日
Hi I would like to interpolate rows of an array with a loop. I wrote this code but it doesn't work, can you help me to solve this problem? Thanks
for i=1:size(A,1)
vq=0.01
b(i,:)=interp1((1:length(A{i,:})),A{i,:},vq)
end
  1 件のコメント
Dave B
Dave B 2021 年 8 月 17 日
What is A (i.e. type, shape)? what are you expecting in b?

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

回答 (1 件)

Yazan
Yazan 2021 年 8 月 18 日
if A is an array, then Matlab will throw an error, as you are using curly braces for indexing.
Below is a demo on how to user interp1
t0 = 1:1:10;
% create a matrix contaning data sampled at step = 1;
t = repmat(t0, [3,1]);
x = (1:3)';
A = x.*t;
% assume you need to sample at a step = 0.5
tq = t0(1):0.5:t0(end);
% interpolate every row linearly and save results in B
B = nan(size(A,1), length(tq));
for j=1:size(A,1)
B(j, :) = interp1(t0, A(j,:), tq);
end
% plot one row of A and B
figure,
plot(tq, B(1,:)); hold on
plot(t0, A(1,:), 's'); grid minor
legend({'Interpolated data', 'Original data'})

カテゴリ

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