user2175783
user2175783

Reputation: 1441

Reshape of function defined matrices

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.

Upvotes: 4

Views: 73

Answers (1)

gnovice
gnovice

Reputation: 125874

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));

Upvotes: 6

Related Questions