How to interpolate from a dataset using interp3?

40 ビュー (過去 30 日間)
Jake
Jake 2023 年 4 月 28 日
編集済み: Stephen23 2023 年 4 月 29 日
Hi,
I have a dataset (4-D complex double) of dimensions 32 x 31 x 21 x 6. The first three parameters 32x31x21 defines two frequencies and heading values, and together theat creates a force variation in 3D space. The last parameter (6) refers to the degrees of freedom. Now, I want to interpolate this data set for a given two frequencies and a heading angle value and obtain the correct complex force value. I have attached the sample dataset here (sampleData.mat), and I'd suppose a sample code would look like the following, but it's not working.
I haven't worked much with interp functions, so I really appreciate your help.
clear; clc;
load('sampleData.mat');
freq1 = 0.01; freq2 = 0.01; heading = 0.01; % Sample values. Simply used for demonstration.
for i = 1:6
x(i,:) = interp3(X, Y, Z, dataset(:,:,:,i), freq1, freq2, heading);
end

採用された回答

Stephen23
Stephen23 2023 年 4 月 29 日
編集済み: Stephen23 2023 年 4 月 29 日
"How to interpolate from a dataset using interp3?"
It is very simple: don't use INTERP3.
"It's completely unlogical"
It is perfectly logical.
It is because users often prefer the array dimensions to correspond to what a matrix/array looks like: the vertical dimension (1) corresponding to the vertical axes (Y), the horizontal dimension (2) corresponding to the horizontal axes (X). But these are swapped around! Yes, they are. And that is exactly how image data are displayed. All image/plotting routines (not just MATLAB) use this interpretation.
Both INTERP2 and INTERP3 use this interpretation of the first two dimensions, because that is what users need.
You can find the TMW attempt at an explanation here:
and here:
Not only MATLAB has to handle this, all applications that process plotted/image data need to consider this. For example, Numpy names these two different interpretations as "ij" and "xy", see the "Notes" here:
Perhaps this could have been avoided if three hundred(?) years ago whoever defined the arbitrary order of matrix/array dimensions had instead specified them as being cols*rows*pages*... , which would probably also have avoided the endless column-major vs row-major flamewars. But too late, we have to work with what we have.
"What can be done to make it logical?"
The very simple solution is to use the correct tool.
For data in ND-grid format (regardless of how many dimensions) like yours you need to use INTERPN:
load('sampleData.mat');
freq1 = 0.01;
freq2 = 0.01;
heading = 0.01;
A = nan(6,1); % better to preallocate
for k = 1:6
A(k,1) = interpn(X, Y, Z, dataset(:,:,:,k), freq1, freq2, heading);
end
disp(A)
1.0e+07 * -0.0225 + 0.0010i 0.0049 - 0.0131i -0.0226 + 0.0077i -0.0763 - 0.0064i -3.2511 + 0.2187i -5.2563 + 0.0678i
Tip for the future: always read the documentation carefully, look at the related functions and topics linked to.
  1 件のコメント
Torsten
Torsten 2023 年 4 月 29 日
I don't do image processing. And for me x corresponds to x, y corresponds to y and z corresponds to z.

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

その他の回答 (1 件)

Torsten
Torsten 2023 年 4 月 28 日
編集済み: Torsten 2023 年 4 月 28 日
It's completely unlogical, but the documentation of interp3 says:
If X, Y, and Z are grid vectors, then size(V) = [length(Y) length(X) length(Z)]
where V corresponds to your 4d array "dataset".
So you will have to modify your code to:
clear; clc;
load('sampleData.mat');
freq1 = 0.01; freq2 = 0.01; heading = 0.01; % Sample values. Simply used for demonstration.
for i = 1:6
x(i,:) = interp3(X, Y, Z, permute(squeeze(dataset(:,:,:,i)),[2 1 3]), freq1, freq2, heading);
end
x
x =
1.0e+07 * -0.0225 + 0.0010i 0.0049 - 0.0131i -0.0226 + 0.0077i -0.0763 - 0.0064i -3.2511 + 0.2187i -5.2563 + 0.0678i
  1 件のコメント
Jake
Jake 2023 年 4 月 29 日
Hi @Torsten! This does get the job done, thank you.
But is there a logical way to achieve this? I understand that a function is needed for interp3 to work, but here it's preset dataset. But I suppose what needs to be done is logical (trilinear interpolation of a pre computed dataset), however, the approach I've specified here is not. What can be done to make it logical?

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by