How do I prepare spreadsheet data for use in interpn?

1 回表示 (過去 30 日間)
Canoe Commuter
Canoe Commuter 2012 年 1 月 17 日
I have an .xls spreadsheet with 294 rows of data. These rows represent multidimensional data, so there are 4 independent variables and 1 dependent variable on each row. I want to interpolate to find dependent variable values anywhere inside the domain.
The number of independent variables is: w = 2 x = 3 y = 7 z = 7 (hence the 2 x 3 x 7 x 7 = 294 rows).
I can sort the data in Excel, but I can't figure out how to load the data for input v required by interpn. I have tried generating a 4D array by loading each "3D column" of z as shown below, but it gives me an error.
RTU_3=xlsread('RTU_3_sim.xlsx','Data IP','A4:AX1183');
w=[75 85];
x=[55 65 75];
y=[60:10:120];
z=[0.7:0.1:1.3];
% Build a 4D array by iterating through the 7 values of z
for i=1:7
j=42*(i-1)+1;
k=42*i;
v(:,:,:,i) = RTU_3(j:k,16);
end
sample_interpolation=interpn(w,x,y,z,v,75.0001,55.0001,60.0001,0.95);
The error is:
??? Error using ==> interpn at 155
Wrong number of input arguments or some dimension of V is less than 2.
But the number of input arguments is 2N+1 as required, and V is shown as a 4-D Double in the workspace.
Can anybody help me understand what's going wrong, and how I should be getting the data into array "v"?
Thanks!

採用された回答

Canoe Commuter
Canoe Commuter 2012 年 1 月 18 日
I got it working. I didn't use reshape(). The real problem was correctly taking the data from the 2D spreadsheet (294x5) and forming a 4D array (2x3x7x7) from it. For other noobs who have the same problem, here's what I did.
I used Excel to sort by column 1, then by column 2, 3 and 4. (The dependent variable is in column 5). Then I put the data into Matlab (as a 2D array "2Ddata") and ran a nested loop to build up the 4D array, as follows:
for ia=1:2
for ib=1:3
for ic=1:7
for id=1:7
row=(ia-1)*147+(ib-1)*49+(ic-1)*7+id;
4D_array(ia,ib,ic,id)=2Ddata(row,5);
end
end
end
end
v=4D_array;
The equation for "row" took a little thinking, but this worked and my interpolation is now giving correct results.
  1 件のコメント
the cyclist
the cyclist 2012 年 1 月 18 日
Since you have it working, I suppose you will not be too interested in pursuing this any further, but you could definitely have done this more efficiently with reshape.
Not 100% sure, but I think the following would be the approach:
Sort your Excel data by columns 4, then 3,2,1 (rather than 1,2,3,4). Then, do
4D_array = reshape(2Ddata(:,5),[2 3 7 7]);
The reason the different sorting order is important is that reshape() fills all the rows down the first column, then each column in turn, then by 3rd and 4th dimension.

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

その他の回答 (1 件)

the cyclist
the cyclist 2012 年 1 月 17 日
This sample code shows how v should be shaped. It needs to mimic the shape of each of your 4-D inputs.
w=[75 85];
x=[55 65 75];
y=[60:10:120];
z=[0.7:0.1:1.3];
v = rand(2,3,7,7);
interpolated_point = interpn(w,x,y,z,v,75.1,55.1,60.1,0.8);
It's a little hard for me to discern how you are trying to build up your v, but I am guessing that the way you are doing it, it does not end up as 2x3x7x7.
  2 件のコメント
Canoe Commuter
Canoe Commuter 2012 年 1 月 17 日
Thanks, cyclist. Your example shows that I'm calling interpn correctly, and that my problem lies in how I'm building v.
My hope was that there's some beautiful matlab magic to take the values from my spreadsheet (a 294x5 matrix) and turn it into a 2x3x7x7 array (with nodes as defined in w,x,y,z). No such luck?
the cyclist
the cyclist 2012 年 1 月 17 日
You can use the reshape() command to do that. If you are careful of the ordering of the input, that should work find for you.

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

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by