Reputation: 2347
In computing the gradient of variable
[dINTRHOdx,dINTrhody,~] = gradient(INTrho, DELTAx, DELTAy, depth);
I get an error
Index exceeds matrix dimensions.
Error in gradient (line 67) g(2:n-1,:) = (f(3:n,:)-f(1:n-2,:)) ./ (h(3:n) - h(1:n-2));
However, the inputs have coherent dimensions:
size(INTrho)
size(DELTAx)
size(DELTAy)
size(depth)
ans =
1080 149 52
ans =
1080 1
ans =
149 1
ans =
52 1
and if I try the following
[dx,dy,~]=gradient(rand(5,5,3),1:5,1:5,1:3)
(gradient computation of variable with coherent dimensions), I get no errors.
Note also that I do not have some variable called gradient:
which gradient
/Applications/MATLAB_R2017a.app/toolbox/matlab/datafun/gradient.m
What could it be the reason of my error?
EDIT: Partial answer
If I make the dimensions exactly the same
DELTAx=repmat(DELTAx, 1,numel(DELTAy),numel(depth));
DELTAy=repmat(DELTAy, size(DELTAx,1),1,numel(depth));
ddepth=repmat(depth, size(DELTAx,1),size(DELTAy,2),1);
then gradient
works
[dINTRHOdx,dINTrhody,~] = gradient(INTrho, DELTAx, DELTAy, ddepth);
But then, why does [dx,dy,~]=gradient(rand(5,5,3),1:5,1:5,1:3)
work?
Upvotes: 0
Views: 67
Reputation: 91
Your problem is the order of your inputs. The reason [dx,dy,~]=gradient(rand(5,5,3),1:5,1:5,1:3)
works is because the first two inputs are both of length 5. A more clear example to see how your input order matters is the following.
[dx,dy,~]=gradient(rand(5,6,3),1:6,1:5,1:3)
If you change the order of your original inputs to the following it should work.
[dINTRHOdx,dINTrhody,~] = gradient(INTrho, DELTAy, DELTAx, depth);
Upvotes: 1