how can I save the data generated from a function that depends on 3 parameters

1 次查看(过去 30 天)
Hello,
I have a function that depends on 3 parameters. I want to create a database by varying each parameter and for this, I will use three for cycles:
For Wpig=3:0.5:8
For chla=0.6:0.2:0.5
For starch_content=2:4:52
[Wavelength,Ea,Es,b,C_abs,C_sca] = proprad(Wpig,chla,starch_content)
Each evaluated condition generates 6 results (Wavelength, Ea, Es, b_C_abs,C_sca). I want to stock these 6 values for each condition, Do you think I should create a mesh/grid for it? It is important for me to be able to use the database created at the end to interpolate.
Could someone help me? I I will be very grateful for your suggestions and advice. Thank you a lot in advance.

采纳的回答

Guillaume
Guillaume 2019-10-2
Assuming the outputs of proprad are all scalar:
[Wpig, chla, starch_content] = ndgrid(3:0.5:8, 0.6:0.2:0.5, 2:4:52);
[Wavelength, Ea, Es, b, C_abs, C_sca] = arrayfun(@proprad, Wpig, chla, starch_content);
If the outputs are not scalar, then add 'UniformOutput', false to the arrayfun call.
That's also assuming that the proprad function cannot operate directly on array inputs. Otherwise, the arrayfun is not necessary.
  12 个评论
Guillaume
Guillaume 2019-10-30
You can use arrayfun and ndgrid as I initially answered:
Wpig = 0.5:0.5:7;
Chla = 0.5:0.01:0.6;
Starch = 0:0.04:0.24;
[Wpig3D, Chla3D, Starch3D] = ndgrid(Wpig, Chla, Starch); %creates 3D matrices
reshapefun = @(w, c, s) reshape(proprad(w, c, s), 1, 1, 1, []); %function to move the vector output of proprad into the 4th dimension
Ea = cell2mat(arrayfun(reshapefun, Wpig3D, Chla3D, Starch3D, 'UniformOutput', false)); %create 3D cell arrays of vectors and convert to matrix
If proprad can directly return a 1x1x1xN vector, then you don't need the reshapefun and can directly do:
Ea = cell2mat(arrayfun(@proprad, Wpig3D, Chla3D, Starch3D, 'UniformOutput', false)); %create 3D cell arrays of vectors and convert to matrix
which will run faster.
Or you can use a loop indeed, which may be faster:
Wpig = 0.5:0.5:7;
Chla = 0.5:0.01:0.6;
Starch = 0:0.04:0.24;
Ea = zeros(numel(Wpig), numel(Chla), numel(starch), 31);
for sidx = 1:numel(Starch)
for cidx = 1:numel(Chla)
for widx = 1:numel(Wpig)
Ea(widx, cidx, sidx, :) = proprad(Wpig(widx), Chla(cidx), Starch(sidx));
end
end
end
Either way, once you've got the 4D matrix, it's easy to interpolate, ndgrid your 4 dimensions (Wpig, Chla, Starch and Wavelength), then build your interpolant with griddedInterpolant and query it at whichever points you want:
Wpig = 0.5:0.5:7;
Chla = 0.5:0.01:0.6;
Starch = 0:0.04:0.24;
Wavelength = linspace(300,700,31);
[Wpig4d, Chla4D, Starch4D, Wavelength4D] = ndgrid(Wpig, Chla, Starch, Wavelength);
%build interpolant
Finterp = griddedInterpolant(Wpgid4D, Chla4D, Starch4D, Wavelength4D, Ea);
%query interpolant for wpig = .93, Chla = 0.57, Starch = 0.12 at all wavelengths:
result = Finterp([repmat([.93, .57, .12], numel(Wavelength), 1), Wavelength(:)])

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by