4

I have the following code in matlab

deltax=@(t)xt(t).'-xt(t);
deltay=@(t)yt(t).'-yt(t);
deltaz=@(t)zt(t).'-zt(t);

deltar=@(t)reshape([deltax(:) deltay(:) deltaz(:)].',3*(100+1),[]).';

where xt, yt, zt are all well defined functions of t. If I do deltax(2), I get a column array with 101 entries and similarly for deltay(2) and deltaz(2).

However when I call

deltar(2)

I get this error

Input arguments to function include colon operator. To input the colon character, use ':' instead.

I have also tried

deltar=@(t)reshape([deltax(t)(:) deltay(t)(:) deltaz(t)(:)].',3*(100+1),[]).';

but this gives me syntax errors.

I must be doing some basic matlab mistake.

Uwe
  • 41,420
  • 11
  • 90
  • 134
user2175783
  • 1,291
  • 1
  • 12
  • 28
  • 4
    Not *exactly* a duplicate, but it's the same problem: `fcn(:)` calls `fcn` with argument '`:`', which is illegal (under most circumstances). What you need to do is something akin to your second attempt, but, that is illegal because this double-indexing is not allowed in MATLAB (it's allowed in Octave, though). You'll have to use `reshape(deltax(t),[],1)` for all three. – Rody Oldenhuis Nov 29 '17 at 16:51
  • 2
    @RodyOldenhuis: That question provides an explanation for the error in the second attempt, and potential solutions (albeit less-than-optimal), but isn't really a duplicate. I think the problem here is just working out a better set of reshaping steps for the data. – gnovice Nov 29 '17 at 17:04
  • 1
    @gnovice well, OK then :) *Related* – Rody Oldenhuis Nov 29 '17 at 17:09

1 Answers1

6

If deltax(t) returns a matrix that you wish to reshape into a column, you can't do it with the colon operator due to having two sets of parentheses immediately following each other (a syntax error in MATLAB; more info can be found here). You'd have to call reshape on each delta(x|y|z) return value individually:

deltar = @(t) reshape([reshape(deltax(t), [], 1) ...
                       reshape(deltay(t), [], 1) ...
                       reshape(deltaz(t), [], 1)].', 3*(100+1), []).';

Alternatively, you can achieve the same reshaping of the data using cat and permute like so:

deltar = @(t) reshape(permute(cat(3, deltax(t), deltay(t), deltaz(t)), [3 1 2]), ...
                      3*(100+1), []).';

And if each delta(x|y|z) always returns a 101-by-M matrix, an even simpler solution is:

deltar = @(t) reshape([deltax(t).'; deltay(t).'; deltaz(t).'], [], 3*(100+1));
gnovice
  • 125,304
  • 15
  • 256
  • 359