Add row vector to column vector

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Add row vector to column vector

Jonathan Seidel
Hello,
I am starting to learn octave. However I stumbled across the following:

I defined a as a row-vector and b as a column vector. Then I added a + b and expected some sort of error message, because the vectors down have the same format. However, I received the following answer. Why ?

>> a = [1 2 3]; b = [1; 2; 3];
>> a + b
ans =

   2   3   4
   3   4   5
   4   5   6

Thanks in advance for your help.
Greetings,
Jonathan


Reply | Threaded
Open this post in threaded view
|

Re: Add row vector to column vector

mmuetzel
Am 27. November 2019 um 16:34 Uhr schrieb "Jonathan Seidel":

> Hello,
> I am starting to learn octave. However I stumbled across the following:
>
> I defined a as a row-vector and b as a column vector. Then I added a + b and expected some sort of error message, because the vectors down have the same format. However, I received the following answer. Why ?
>
> >> a = [1 2 3]; b = [1; 2; 3];
> >> a + b
> ans =
>
>    2   3   4
>    3   4   5
>    4   5   6
>
> Thanks in advance for your help.

Octave uses automatic broadcasting. See also section 19.2 of the manual.

Markus


Reply | Threaded
Open this post in threaded view
|

Re: Add row vector to column vector

BGreen
In reply to this post by Jonathan Seidel

On Wed, Nov 27, 2019 at 10:43 AM Jonathan Seidel <[hidden email]> wrote:
Hello,
I am starting to learn octave. However I stumbled across the following:

I defined a as a row-vector and b as a column vector. Then I added a + b and expected some sort of error message, because the vectors down have the same format. However, I received the following answer. Why ?

>> a = [1 2 3]; b = [1; 2; 3];
>> a + b
ans =

   2   3   4
   3   4   5
   4   5   6

Thanks in advance for your help.
Greetings,
Jonathan

Octave "fills in the gaps" and adds
1 2 3
1 2 3
1 2 3
to
1 1 1
2 2 2
3 3 3
giving you the result above. Perhaps someone else who's been a user longer than I have can explain the motivation for having Octave process the input this way rather than giving an error.


Reply | Threaded
Open this post in threaded view
|

Re: Add row vector to column vector

James Sherman
In reply to this post by Jonathan Seidel

On Wed, Nov 27, 2019 at 10:43 AM Jonathan Seidel <[hidden email]> wrote:
Hello,
I am starting to learn octave. However I stumbled across the following:

I defined a as a row-vector and b as a column vector. Then I added a + b and expected some sort of error message, because the vectors down have the same format. However, I received the following answer. Why ?

>> a = [1 2 3]; b = [1; 2; 3];
>> a + b
ans =

   2   3   4
   3   4   5
   4   5   6

Thanks in advance for your help.
Greetings,
Jonathan



Hi Jonathan,


If you want to make sure they're the same size, you'll need to check to see if size(a) is equal to size(b).

Hope this helps,

James Sherman Jr.