Reputation: 99
I have a 7426x32 2D array of data from a 1deg cylindrical slice. It contains: X,Y,Z, and a 29 variables such as temperature, density, pressure, etc. There are 79 mesh nodes in the X direction, 2 in the Y, and 47 in the Z. It's one 'line' of cell volumes in the Y direction. The data is written out in this manner:
do Z = 1,47
do Y = 1,2
do X = 1,79
X,Y,Z, then the other 29 variables at each node
enddo
enddo
enddo
I need to interpolate the variables to get the average value of a variable in each cell volume (which makes 3713 values), multiply each to the volume contained in each cell, and then sum that all up. I understand that I can use interp3 function in Matlab, but I'm not really certain how to get the function to work with the data that I have.
'Gut instinct' tells me I should have:
Vq = interp3(A(1:79,1),A(1:79,2),A(1:79,3),A(1:79,30))
To get the variable in the 30th column. But it's not really working, and I'm not sure how it works with the way my data is arranged. Could anyone point me in the right direction?
Thank you very much!
Upvotes: 1
Views: 687
Reputation: 1635
The trick is that the interp
family of functions takes the input coordinates and values in meshgrid
format, so in this case the first three arguments would have to be 3D matrices of the coordinate space.
You can reconstruct this using meshgrid
:
[X,Y,Z] = meshgrid(1:79, 1:2, 1:47);
each of X, Y and Z will then be a 79x2x47 matrix representing the respective coordinate.
So you could construct this directly from what you know of the input data but it's more robust and general to use reshape
directly on the data, particularly as you will have to perform the same procedure on the 30th column to put it into the same format (you still have to know how many coordinate points you have but it's possible to work that out programmatically for unknown datasets).
Give this a try:
X = reshape(A(:,1), 79, 2, 47);
Y = reshape(A(:,2), 79, 2, 47);
Z = reshape(A(:,3), 79, 2, 47);
col30 = reshape(A(:,30), 79, 2, 47);
Vq = interp3(X,Y,Z,col30,Xq,Yq,Zq);
where Xq, Yq and Zq are the test coordinates, which could be vectors or scalars - they don't need to be in meshgrid format. Bear in mind you'll need to call interp3 for each of your 29 variables.
It may not work immediately - the coordinate ordering is important and I'm not confident I've properly converted your description of the variables into Matlab's form (it does column-wise indexing so sometimes matrices represent YXZ data but other times XYZ rules apply and I always like to check the data to make sure I've got them right). If not examine the matrices manually to make sure they are ordered correctly and permute the order of the 79, 2, 47 arguments in reshape
as appropriate.
Upvotes: 1