Simultaneously interpolating over multiple rows of a matrix.

I have a matrix ("mat") of size N*M where each row is defined over a grid ("grid"), and I would like to obtain interpolated values ("vals") over a subset of rows of length K ("ind1") over a query vector ("qvec"). This is part of a larger iterative routine, where the size and elements of ind1 and the elements of mat and qvec change each iteration. I'm currently using a for-loop over the subset of rows, interpolating one at time. Here is a simplified example:
K = length(ind1);
vals_temp = zeros(N,1);
for ii = 1:K
ind2 = ind1(ii);
vals_temp(ind2,1) = interp1(grid,mat(ind2,:),qvec(ind2));
end
vals = vals_temp(vals_temp>0);
I would like to be able to do this without a loop because this is very time consuming. I’m thinking that I can use interpn over the entire subset ind1, but I’m having trouble figuring out how to make the inputs conformable. Any help would be appreciated.

 採用された回答

Matt J
Matt J 2021 年 4 月 7 日
編集済み: Matt J 2021 年 4 月 7 日

0 投票

This might be better,
e=1:K;
F=griddedInterpolant(mat(ind1,:),{e,grid});
vals_temp = F(e,qvec);

2 件のコメント

Brandon
Brandon 2021 年 4 月 7 日
I get this error:
Error using griddedInterpolant
Sample values must be of type double or single.
Brandon
Brandon 2021 年 4 月 7 日
it appears the inputs to griddedInterpolant above are reversed:
F=griddedInterpolant({e,grid},mat(ind1,:));
Now there is about a 6% improvement in computational time. Thanks!

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

その他の回答 (1 件)

Matt J
Matt J 2021 年 4 月 7 日
編集済み: Matt J 2021 年 4 月 7 日

0 投票

V=mat.';
vals_temp = interp1(grid,V(:,ind1),qvec);

5 件のコメント

Brandon
Brandon 2021 年 4 月 7 日
Thanks for you help. I think that something might still be missing, as this gives me a K*K matrix instead of a K length vector.
Matt J
Matt J 2021 年 4 月 7 日
Is the length of qvec also K?
Brandon
Brandon 2021 年 4 月 7 日
I just need to take the diagonals of that K*K matrix to get what i'm looking for. Thanks a lot!
Matt J
Matt J 2021 年 4 月 7 日
OK, but wait, now you're doing K^2 interpolation operations instead of K operations. Are you finding this to be faster regardless?
Brandon
Brandon 2021 年 4 月 7 日
Computation time is actually about 5% longer. Any other suggestions?

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

カテゴリ

ヘルプ センター および File ExchangeInterpolation についてさらに検索

タグ

質問済み:

2021 年 4 月 7 日

コメント済み:

2021 年 4 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by