Quantcast

Differences between Octave and Matlab on division (/) operator with scalar and vector

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Differences between Octave and Matlab on division (/) operator with scalar and vector

José Luis García Pallero
Hello:

Using this variables:

a = 2;
b = [1 2 3];

For the operation a/b or 2/b or b/2 I obtain an error in Octave and
Matlab due to wrong dimensions. But if I declare b as a vector column
as b=[1 2 3]' I obtain in Octave:

>> a/b
ans =

   0.14286   0.28571   0.42857

and in Matlab:

>> a/b

ans =

         0         0    0.6667

What is the reason of this difference? Why the operation is legal with
b as column vector and illegal with row vector?

Thanks

--
*****************************************
José Luis García Pallero
[hidden email]
(o<
/ / \
V_/_
Use Debian GNU/Linux and enjoy!
*****************************************

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Differences between Octave and Matlab on division (/) operator with scalar and vector

Sebastian Schöps
José Luis García Pallero wrote
Hello:

Using this variables:

a = 2;
b = [1 2 3];

For the operation a/b or 2/b or b/2 I obtain an error in Octave and
Matlab due to wrong dimensions. But if I declare b as a vector column
as b=[1 2 3]' I obtain in Octave:

>> a/b
ans =

   0.14286   0.28571   0.42857

and in Matlab:

>> a/b

ans =

         0         0    0.6667

What is the reason of this difference? Why the operation is legal with
b as column vector and illegal with row vector?

Thanks
Your problem does not have a unique solutions and Octave favors a different (sparse) one. I had recently also problems with this issues, including the different handling w.r.t. dimensions. You find the discussion here: http://octave.1599824.n4.nabble.com/Bug-when-dividing-a-scalar-by-a-vector-td4681411.html

Sebastian
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Differences between Octave and Matlab on division (/) operator with scalar and vector

Juan Pablo Carbajal-2
In reply to this post by José Luis García Pallero
On Sat, Mar 25, 2017 at 6:48 PM, José Luis García Pallero
<[hidden email]> wrote:

> Hello:
>
> Using this variables:
>
> a = 2;
> b = [1 2 3];
>
> For the operation a/b or 2/b or b/2 I obtain an error in Octave and
> Matlab due to wrong dimensions. But if I declare b as a vector column
> as b=[1 2 3]' I obtain in Octave:
>
>>> a/b
> ans =
>
>    0.14286   0.28571   0.42857
>
> and in Matlab:
>
>>> a/b
>
> ans =
>
>          0         0    0.6667
>
> What is the reason of this difference? Why the operation is legal with
> b as column vector and illegal with row vector?
>
> Thanks
>
> --
> *****************************************
> José Luis García Pallero
> [hidden email]
> (o<
> / / \
> V_/_
> Use Debian GNU/Linux and enjoy!
> *****************************************
>
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-octave
In Octave a/b when b is a matrix (any size), is equivalent to
a*inv(b), in this case a*pinv(b) (try it out).
I can't see what matlab is doing, but the last element is 2/3.

I also do not understand why there is an error in the first case,
should given the same result...i.e. apply a*pinv(b) when b is a row
vector

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Differences between Octave and Matlab on division (/) operator with scalar and vector

James Sherman
On Sat, Mar 25, 2017 at 2:51 PM, Juan Pablo Carbajal
<[hidden email]> wrote:

> On Sat, Mar 25, 2017 at 6:48 PM, José Luis García Pallero
> <[hidden email]> wrote:
>> Hello:
>>
>> Using this variables:
>>
>> a = 2;
>> b = [1 2 3];
>>
>> For the operation a/b or 2/b or b/2 I obtain an error in Octave and
>> Matlab due to wrong dimensions. But if I declare b as a vector column
>> as b=[1 2 3]' I obtain in Octave:
>>
>>>> a/b
>> ans =
>>
>>    0.14286   0.28571   0.42857
>>
>> and in Matlab:
>>
>>>> a/b
>>
>> ans =
>>
>>          0         0    0.6667
>>
>> What is the reason of this difference? Why the operation is legal with
>> b as column vector and illegal with row vector?
>>
>> Thanks
>>
>> --
>> *****************************************
>> José Luis García Pallero
>> [hidden email]
>> (o<
>> / / \
>> V_/_
>> Use Debian GNU/Linux and enjoy!
>> *****************************************
>>
>> _______________________________________________
>> Help-octave mailing list
>> [hidden email]
>> https://lists.gnu.org/mailman/listinfo/help-octave
> In Octave a/b when b is a matrix (any size), is equivalent to
> a*inv(b), in this case a*pinv(b) (try it out).
> I can't see what matlab is doing, but the last element is 2/3.
>
> I also do not understand why there is an error in the first case,
> should given the same result...i.e. apply a*pinv(b) when b is a row
> vector
>
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-octave

I just tried this on MATLAB version 2016a, and got the following results:

>> a = 2;
>> b = [1 2 3];
>> 2/b
Error using  /
Matrix dimensions must agree.
>> a/b
Error using  /
Matrix dimensions must agree.
>> b/2
ans =
    0.5000    1.0000    1.5000

Are you sure that b/2 caused an error?  That is well defined and
should produce elementwise division by 2.

James Sherman

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Differences between Octave and Matlab on division (/) operator with scalar and vector

José Luis García Pallero
2017-03-25 20:03 GMT+01:00 James Sherman Jr. <[hidden email]>:

> On Sat, Mar 25, 2017 at 2:51 PM, Juan Pablo Carbajal
> <[hidden email]> wrote:
>> On Sat, Mar 25, 2017 at 6:48 PM, José Luis García Pallero
>> <[hidden email]> wrote:
>>> Hello:
>>>
>>> Using this variables:
>>>
>>> a = 2;
>>> b = [1 2 3];
>>>
>>> For the operation a/b or 2/b or b/2 I obtain an error in Octave and
>>> Matlab due to wrong dimensions. But if I declare b as a vector column
>>> as b=[1 2 3]' I obtain in Octave:
>>>
>>>>> a/b
>>> ans =
>>>
>>>    0.14286   0.28571   0.42857
>>>
>>> and in Matlab:
>>>
>>>>> a/b
>>>
>>> ans =
>>>
>>>          0         0    0.6667
>>>
>>> What is the reason of this difference? Why the operation is legal with
>>> b as column vector and illegal with row vector?
>>>
>>> Thanks
>>>
>>> --
>>> *****************************************
>>> José Luis García Pallero
>>> [hidden email]
>>> (o<
>>> / / \
>>> V_/_
>>> Use Debian GNU/Linux and enjoy!
>>> *****************************************
>>>
>>> _______________________________________________
>>> Help-octave mailing list
>>> [hidden email]
>>> https://lists.gnu.org/mailman/listinfo/help-octave
>> In Octave a/b when b is a matrix (any size), is equivalent to
>> a*inv(b), in this case a*pinv(b) (try it out).
>> I can't see what matlab is doing, but the last element is 2/3.
>>
>> I also do not understand why there is an error in the first case,
>> should given the same result...i.e. apply a*pinv(b) when b is a row
>> vector
>>
>> _______________________________________________
>> Help-octave mailing list
>> [hidden email]
>> https://lists.gnu.org/mailman/listinfo/help-octave
>
> I just tried this on MATLAB version 2016a, and got the following results:
>
>>> a = 2;
>>> b = [1 2 3];
>>> 2/b
> Error using  /
> Matrix dimensions must agree.
>>> a/b
> Error using  /
> Matrix dimensions must agree.
>>> b/2
> ans =
>     0.5000    1.0000    1.5000
>
> Are you sure that b/2 caused an error?  That is well defined and
> should produce elementwise division by 2.

Sorry, it was a mistake. b/2 peoduces correct results

>
> James Sherman



--
*****************************************
José Luis García Pallero
[hidden email]
(o<
/ / \
V_/_
Use Debian GNU/Linux and enjoy!
*****************************************

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Loading...