How to interpolate from a dataset using interp3?
28 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Stephen23
on 29 Apr 2023
Edited: Stephen23
on 29 Apr 2023
"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)
Tip for the future: always read the documentation carefully, look at the related functions and topics linked to.
1 Comment
Torsten
on 29 Apr 2023
I don't do image processing. And for me x corresponds to x, y corresponds to y and z corresponds to z.
More Answers (1)
Torsten
on 28 Apr 2023
Edited: Torsten
on 28 Apr 2023
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
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!