2 views (last 30 days)

I know that some don't like implicit expansion. Given that it's likely here to stay, should it be made applicable to some additional functions? Just the other day I ran into a case like this, where one of X or Y is n x 3 and the other 1 x 3.

Z = X + Y;

Z = X - Y;

Z = dot(X,Y,2); % doesn't work

Is there some fundamental reason why dot can't support implicit expansion? If not, should it? Are there other functions, like cross, that should support implicit expansion?

Intresting to me is that the doc says: "Most binary (two-input) operators and functions in MATLAB® support numeric arrays that have compatible sizes. Two inputs have compatible sizes if, for every dimension, the dimension sizes of the inputs are either the same or one of them is 1." So most binary functions, but not all, support it. It would be nice if the doc had a single list of functions that support implicit expansion, or, if shorter, a list of binary functions that do not.

Walter Roberson
on 9 Oct 2020

Yes, it should be extended to datetime() objects.

>> datetime() + hours(1:5).' + hours(1:3)

Data inputs must be the same size, or any of them can be a scalar.

>> hours(1:5).' + hours(1:3) + datetime()

ans =

5×3 datetime array

09-Oct-2020 19:05:40 09-Oct-2020 20:05:40 09-Oct-2020 21:05:40

09-Oct-2020 20:05:40 09-Oct-2020 21:05:40 09-Oct-2020 22:05:40

09-Oct-2020 21:05:40 09-Oct-2020 22:05:40 09-Oct-2020 23:05:40

09-Oct-2020 22:05:40 09-Oct-2020 23:05:40 10-Oct-2020 00:05:40

09-Oct-2020 23:05:40 10-Oct-2020 00:05:40 10-Oct-2020 01:05:40

>> bsxfun(@plus,(datetime() + hours(1:5)).', hours(1:3))

Error using bsxfun

Operands must be numeric arrays.

Steven Lord
on 9 Oct 2020

Matt J
on 10 Oct 2020

I wouldn't mind seeing it extended to the colon operator, instead of having to do things like this,

a=2;

b=[5,7,9];

c=arrayfun(@(q)a:q, b,'uni',0); %I'd rather just do a:b

c{:}

Matt J
on 11 Oct 2020

I don't think I have ever seen a good reason to permit non-scalars in the colon operator.

I suppose I was really thinking that it might be good if combined with automatic comma separated list expansion. So, for example, instead of extracting a sub-array given its lower and upper limit vectors a and b,

M(a(1):2:b(1),a(2):2:b(2),a(3):2:b(3))

I could instead just do,

M(a:2:b)

Steven Lord
on 11 Oct 2020

I'm not completely certain, but I suspect Bruno Luong's answer is close to the mark. The colon operator dates back to a very early version of MATLAB, probably even Cleve's original Fortran MATLAB. I suspect Cleve didn't consider that people would use : with non-scalar values and so didn't check for that.

We've learned since then. Were we to redesign : today, we likely wouldn't allow non-scalar inputs without a good use case.

Matt J
on 11 Oct 2020

We've learned since then. Were we to redesign : today, we likely wouldn't allow non-scalar inputs without a good use case.

In addition to the one I raised above, I think that a good case would in the use of griddedInterpolant. Instead of upsampling an array V by doing,

F=griddedInterpolant(V);

[m,n,p]=size(V);

Vq=F({1:0.5:m,1:0.5:n,1:0.5:p})

it would be much more sleek to do,

Vq=F({1:0.5:size(V)})

Opportunities for recent engineering grads.

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

Start Hunting!
## 13 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/609371-should-implicit-expansion-be-applied-to-additional-functions#comment_1048006

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/609371-should-implicit-expansion-be-applied-to-additional-functions#comment_1048006

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/609371-should-implicit-expansion-be-applied-to-additional-functions#comment_1048451

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/609371-should-implicit-expansion-be-applied-to-additional-functions#comment_1048451

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/609371-should-implicit-expansion-be-applied-to-additional-functions#comment_1048481

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/609371-should-implicit-expansion-be-applied-to-additional-functions#comment_1048481

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/609371-should-implicit-expansion-be-applied-to-additional-functions#comment_1048596

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/609371-should-implicit-expansion-be-applied-to-additional-functions#comment_1048596

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/609371-should-implicit-expansion-be-applied-to-additional-functions#comment_1048606

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/609371-should-implicit-expansion-be-applied-to-additional-functions#comment_1048606

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/609371-should-implicit-expansion-be-applied-to-additional-functions#comment_1048681

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/609371-should-implicit-expansion-be-applied-to-additional-functions#comment_1048681

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/609371-should-implicit-expansion-be-applied-to-additional-functions#comment_1049331

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/609371-should-implicit-expansion-be-applied-to-additional-functions#comment_1049331

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/609371-should-implicit-expansion-be-applied-to-additional-functions#comment_1049391

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/609371-should-implicit-expansion-be-applied-to-additional-functions#comment_1049391

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/609371-should-implicit-expansion-be-applied-to-additional-functions#comment_1049886

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/609371-should-implicit-expansion-be-applied-to-additional-functions#comment_1049886

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/609371-should-implicit-expansion-be-applied-to-additional-functions#comment_1051216

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/609371-should-implicit-expansion-be-applied-to-additional-functions#comment_1051216

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/609371-should-implicit-expansion-be-applied-to-additional-functions#comment_1051341

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/609371-should-implicit-expansion-be-applied-to-additional-functions#comment_1051341

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/609371-should-implicit-expansion-be-applied-to-additional-functions#comment_1051366

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/609371-should-implicit-expansion-be-applied-to-additional-functions#comment_1051366

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/609371-should-implicit-expansion-be-applied-to-additional-functions#comment_1051701

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/609371-should-implicit-expansion-be-applied-to-additional-functions#comment_1051701

Sign in to comment.