Resample function with splitapply limitation?
3 views (last 30 days)
Show older comments
Daniel Goldstein
on 3 Jan 2022
Commented: Daniel Goldstein
on 6 Jan 2022
I'm working with a drilling datset.
The rate of penetration 'rop' is acquired non-uniformly and recorded as a depth (in meters). Depth and rop are doubles. I have the hole names as categories.
I've been able to uniformly resample one hole's rop and depth at the the desired depth spacing (0.1m).
e.g. hole12's first meter using function [y,ty] = resample(yg,t,fs);
t = original depth
yg = original rop
y = resampled rop
ty = resampled depth
fs = frequency resampled
holeid = [12;12;12;12;12;12;12;12;12;12;12;12;12;12;12;12];
depth = [0;0.0542;0.0801;0.2222;0.3959;0.4572;0.5110;0.5348;0.5712;0.6099;0.6437;0.6799;0.8011;0.8928;0.9590;1.0110];
rop = [118.7000;207.8000;139.6000;176.0000;177.8000;229.3000;242.4000;138.9000;85.7000;140.8000;164.5000;125.4000;189.8000;164.0000;118.4000;211.4000];
hole12 = table(holeid,depth,rop)
fs = 10;
[y,yt] = func(hole12.rop,hole12.depth,fs)
plot(hole12.rop,hole12.depth);
hold on
plot(y,yt);
hold off
legend({'original','resampled'})
xlabel('rop')
ylabel('depth')
set(gca, 'YDir','reverse')
Unfortunately, it doesn't seem to work for multiple holes when using the resample function and splitapply to apply the function to each group i.e. hole to yield a uniformly resampled depth and 'rop'.
holeid = [12;12;12;12;12;12;12;12;12;12;12;12;12;12;12;12;13;13;13;13;13;13;13;13;13;13;13;13;13;13;13;13;13;13;13;13];
depth = [0;0.0542;0.0801;0.2222;0.3959;0.4572;0.5110;0.5348;0.5712;0.6099;0.6437;0.6799;0.8011;0.8928;0.9590;1.0110;0;0.0432;0.1334;0.2483;0.4087;0.4443;0.5009;0.5567;0.5890;0.6253;0.6923;0.7274;0.7588;0.8296;0.8623;0.8955;0.9272;0.9652;0.9959;1.0218];
rop = [118.7000;207.8000;139.6000;176.0000;177.8000;229.3000;242.4000;138.9000;85.7000;140.8000;164.5000;125.4000;189.8000;164.0000;118.4000;211.4000;148.8000;227.9000;14.6000;220.1000;177.2000;226.0000;144.5000;114.2000;157.8000;207.8000;242.5000;247.5000;238.7000;245.7000;241.4000;244.7000;235.6000;242.7000;239.1000;226.0000];
holes12and13 = table(holeid,depth,rop)
holes12and13.holeid = categorical(holes12and13.holeid)
G = findgroups(holes12and13.holeid)
[y1,ty1] = splitapply(func,holes12and13.rop,holes12and13.depth,fs,holes12and13.rop,G)
function [y,yt] = func(yg,t,fs);
[y,yt] = resample(yg,t,fs)
end
Is this a syntax issue calling the function inputs or a limitation of splitapply because it can only apply the function to one input at a time (e.g. 'x') (instead of the three required for the resample function work e.g. 'yg','t' and 'fs')? Is there another way to accomplish this e.g. a loop and how would that be done based on groupds of categorical data?
0 Comments
Accepted Answer
Walter Roberson
on 3 Jan 2022
Your fundamental problem was that you were calling splitapply(func, ...) which causes the function func to be evaluated with no inputs, and the expectation that func() will return a function handle that will be passed into splitapply(). You need to pass in @func to use func as the function handle to pass to split apply.
But you have other problems. You do not define fs . Presumably it is a scalar. But splitapply() requires that all of the parameters have the same number of rows as it splits all of them .
If you use
splitapply(@func,holes12and13.rop,holes12and13.depth,fs,holes12and13.rop,G)
then you are telling splitapply() that @func expects 4 inputs -- the rop, the depth, the fs, and the rop again, as four independent parameters. But your func only accepts three inputs.
You should probably be reading http://www.mathworks.com/help/matlab/math/parameterizing-functions.html
holeid = [12;12;12;12;12;12;12;12;12;12;12;12;12;12;12;12;13;13;13;13;13;13;13;13;13;13;13;13;13;13;13;13;13;13;13;13];
depth = [0;0.0542;0.0801;0.2222;0.3959;0.4572;0.5110;0.5348;0.5712;0.6099;0.6437;0.6799;0.8011;0.8928;0.9590;1.0110;0;0.0432;0.1334;0.2483;0.4087;0.4443;0.5009;0.5567;0.5890;0.6253;0.6923;0.7274;0.7588;0.8296;0.8623;0.8955;0.9272;0.9652;0.9959;1.0218];
rop = [118.7000;207.8000;139.6000;176.0000;177.8000;229.3000;242.4000;138.9000;85.7000;140.8000;164.5000;125.4000;189.8000;164.0000;118.4000;211.4000;148.8000;227.9000;14.6000;220.1000;177.2000;226.0000;144.5000;114.2000;157.8000;207.8000;242.5000;247.5000;238.7000;245.7000;241.4000;244.7000;235.6000;242.7000;239.1000;226.0000];
holes12and13 = table(holeid,depth,rop)
holes12and13.holeid = categorical(holes12and13.holeid)
G = findgroups(holes12and13.holeid)
fs = 8000;
[y1,ty1] = splitapply(@func,holes12and13.rop,holes12and13.depth,fs,holes12and13.rop,G)
function [y,yt] = func(yg,t,fs);
whos
which resample
nargin
[y,yt] = resample(yg,t,fs)
end
8 Comments
Walter Roberson
on 6 Jan 2022
holeid = [12;12;12;12;12;12;12;12;12;12;12;12;12;12;12;12;13;13;13;13;13;13;13;13;13;13;13;13;13;13;13;13;13;13;13;13];
depth = [0;0.0542;0.0801;0.2222;0.3959;0.4572;0.5110;0.5348;0.5712;0.6099;0.6437;0.6799;0.8011;0.8928;0.9590;1.0110;0;0.0432;0.1334;0.2483;0.4087;0.4443;0.5009;0.5567;0.5890;0.6253;0.6923;0.7274;0.7588;0.8296;0.8623;0.8955;0.9272;0.9652;0.9959;1.0218];
rop = [118.7000;207.8000;139.6000;176.0000;177.8000;229.3000;242.4000;138.9000;85.7000;140.8000;164.5000;125.4000;189.8000;164.0000;118.4000;211.4000;148.8000;227.9000;14.6000;220.1000;177.2000;226.0000;144.5000;114.2000;157.8000;207.8000;242.5000;247.5000;238.7000;245.7000;241.4000;244.7000;235.6000;242.7000;239.1000;226.0000];
fs10 = [10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10];
holes12and13 = table(holeid,depth,rop,fs10);
fs = 10;
G = findgroups(holes12and13.holeid);
[y1,ty1] = splitapply(@(r,d)func(r,d,fs), holes12and13.rop, holes12and13.depth,G);
y1, ty1
function [y,yt] = func(yg,t,fs);
[ty,tyt] = resample(yg,t,fs);
y = {ty}; yt = {tyt};
end
More Answers (0)
See Also
Categories
Find more on Detection 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!