# X, Y, Z vectors to image (Error: Assignment has fewer non-singleton rhs dimensions than non-singleton subscripts)

1 view (last 30 days)
Varoujan on 30 Sep 2015
Answered: Walter Roberson on 30 Sep 2015
I have three very large vectors (30 million points each). X, Y and T. I'd like to turn this into an image.
X has the x-coordinates
Y has the y-coordinates
T has the time values
All three vectors are ordered the same way. What I want in the end:
Img(Y(i), X(i)) = T(i).
Img(round(y0(1)),round(x0(1))) = t1(1); % works
Img(round(y0),round(x0)) = t1; % doesn't work and gives the non-singleton error message.

Walter Roberson on 30 Sep 2015
maxx = max(X);
maxy = max(Y);
img(sub2ind([maxy, maxx], X, Y)) = T;
which can also be coded as
maxy = max(Y);
img((X-1)*maxy + Y) = T;
The problem you ran into is that img([1 2],[1 2]) does not mean img(1,1) and img(2,2): it means img(1,1), img(2,1), img(1,2), img(2,2) -- that is every combination of the index vectors. That is so that constructs such as img(1:3,X) work and give column X in each of rows 1, 2, 3, no matter how many entries there are in X, as your code would be equivalent to saying that img(1:3,X) should act as expected in row/column combination ranges if X had any number of elements other than 3, but that if X happened to have 3 elements then suddenly the meaning should shift to img(1,X(1)), img(2,X(2)), img(3,X(3)); that is of course not correct.