Info

This question is closed. Reopen it to edit or answer.

Error: Assignment has more non-singleton rhs dimensions than non-singleton subscripts

1 view (last 30 days)
Hello all,
I tried to run a particle tracking model on matlab. When i try to run the code below:
clear I J u v u1 u2 v1 v2
I = find(xq<x(it,ip),1);
J = find(yq<y(it,ip),1);
u1 = MASK(J,I).*U(J,I);
v1 = MASK(J,I).*V(J,I);
% advection:
ddx = u1*1800;%m.DT;
ddy = v1*1800;%m.DT;
an error message appears saying:
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Error in PTM_northMenai - Copy (line 141)
x(it+1,ip) = x(it,ip)+ ddx;
However if i change the sign < for > it works properly!! I would like to know if someone could explain it to me and how could I fix it?
Thank you very much all
  1 Comment
Jonathan Demmer
Jonathan Demmer on 21 Aug 2018
clear I J u v u1 u2 v1 v2
I = find(xq<x(it,ip),1);
J = find(yq<y(it,ip),1);
u1 = MASK(J,I).*U(J,I);
v1 = MASK(J,I).*V(J,I);
% advection:
ddx = u1*1800;%m.DT;
ddy = v1*1800;%m.DT;

Answers (2)

Walter Roberson
Walter Roberson on 21 Aug 2018
If the find() does not find anything then it returns empty, which results in empty when used as an index, which results in ddx and ddy being empty. When you add the empty ddx to a value you get empty. You then try to store the empty results in a location with size 1, but that doesn't fit.
  4 Comments
Walter Roberson
Walter Roberson on 22 Aug 2018
Instead of looping over it and ip, you can use something like
[~, xbin] = histc(x, qx);
[~, ybin] = histc(y, qy);
ind = sub2ind(size(MASK), ybin, xbin);
u1 = MASK(ind) .* U(ind);
v1 = MASK(ind) .* V(ind);
ddx = u1*1800;%m.DT;
ddy = v1*1800;%m.DT;
Watch out for the boundary conditions: x values outside the range xmin to xmax is obvious, but the behaviour at xmax can be a problem. histc() gives the last entry a bin all of its own.
The newer
[~, ~, xbin] = histcounts(x, qx);
would treat the upper bound exactly as part of the previous bin.

Jonathan Demmer
Jonathan Demmer on 22 Aug 2018
I know sorry but the code is really long that is why i did not copy it...
xq and yq are define as: dx = 50; dy = 50; xq = xmin : dx : xmax; yq = ymin : dy : ymax;
Where xmin, xmax, ymin and ymax are UTM coordinates value

Community Treasure Hunt

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

Start Hunting!