3

I use the + operator in my r2017a version to sum a line vector and a row vector to give me an array.

A = [1 2]

B = [1;2]

C = A + B = [2 3; 3 4]

I tried to run my code on an other computer with the r2016a version but it doesn't work.

How can I simply do this command?

GuillaumeC
  • 43
  • 3

2 Answers2

3

MATLAB introduced a new feature in r2016b that automatically expands the matrices to required dimensions while performing arithmetic operations. You can read more about it here. Hence, your code does not work in r2016a. The way to do it in r2016a is this

C = bsxfun(@plus, A, B);
ammportal
  • 991
  • 1
  • 11
  • 27
  • Do you know if it affects the speed of the execution by using bsxfun instead of the "new" + ? – GuillaumeC May 08 '17 at 13:39
  • I am not sure, but the new '+' should be generally faster as it is more integrated with the matlab syntax. Although i doubt there will be a significant difference. I don't have the new MATLAB version so cannot check it out. You can though by using tic, and toc. – ammportal May 08 '17 at 13:50
  • 3
    @GuillaumeC Speed is similar; see [here](http://stackoverflow.com/q/42559922/2586922). Or do a similar test for your specific case (and in that case use `timeit` for more precise timing, rather than `tic`, `toc`) – Luis Mendo May 08 '17 at 14:05
0

i don't have r2017 and this operation is logically wrong i think you meant to do this

C=[A+B(1);A+B(2)]

and it is definitely faster than functions

you can use for loop for higher dimentions

for i=1:size(b,1)        
C=[A+B(i);A+B(i)];
end
Reflection
  • 399
  • 2
  • 11
  • Unfortunately, I use vectors of length x to build x*x array in a 4D (x,x,x,x) array. The x parameter can be between 5 and 100. So, I have to find a generic solution. – GuillaumeC May 08 '17 at 14:01
  • @Reflection I previously used loops, but in my case, the + operator (from r2017a) is way more faster. – GuillaumeC May 08 '17 at 14:10
  • @GuillaumeC, yes, it is true, operators are much faster, but in your case it is not cross version, – Reflection May 08 '17 at 14:20
  • @Luis Mendo i meant it is not defined in mathematics – Reflection May 08 '17 at 14:21
  • Oh, I see. But it _is_ logical from a programming point of view (for example, it also exists in Numpy, where it is called broadcast). Perhaps you could change "logically wrong" by some other, less strong statement. Anyway, it's up to you :-) – Luis Mendo May 08 '17 at 14:54