Octave and Matlab "wat" matrix/vector inconsistencies -


i've noticed various cases in matlab , octave functions accept both matrices , vectors, doesn't same thing vectors matrices.

this can frustrating because when input matrix variable number of rows/columns, interpreted vector , don't expect when height/width 1 making difficult debugging , weird conditional edge cases.

i'll list few i've found, i'm curious others people have run into

(note: i'm looking cases code accepts matrices valid input. raises exception when non-vector matrix given argument doesn't count)

1) "diag" can used mean diagonal of matrix or turn vector diagonal matrix

since former used square matrices isn't egregious in matlab, in octave can particularly painful when octave interperets vector beginning nonzero element , else zeros "diagonal matrix" ie

t=eye(3); size(diag(t(:,3))) == [3,3] size(diag(t(:,2))) == [3,3] size(diag(t(:,1))) == [1,1] 

2) indexing row-vector logicals returns row-vector

indexing else logicals returns column vector

a = 1:3; b = true(1,3); size(a(b)) == [1, 3] = [a; a]; b = [b; b]; size(a(b)) == [6, 1] 

3) indexing vector v index vector returns vector of same (row/col) type v. if either v or matrix, return value has same size i.

a = 1:3; b = a'; size(a(b)) == [1, 3] b = [b,b]; size(a(b)) == [3, 2] 

4) max, min, sum etc. operate on columns of matrix m individiually unless m 1xn in case operate on m single row-vector

a = 1:3 size(max(a)) == [1, 1] = [a;a] size(max(a)) == [1, 3] 

max particularly bad since can't take dimension argument (unlike sum)

what other such cases should watch out when writing octave/matlab code?

each language has own concepts. important point of language think of matrices array of vectors, each column entry. things start make sense then. if don't want behavior, use matrix(:) argument functions pass single vector, rather matrix. example:

octave> = magic (5); octave> max (a) ans =     23   24   25   21   22  octave> max (a(:)) ans =  25 

1) not true @ least octave 3.6.4. i'm not 100% sure may related related this bug has been fixed.

2) if index boolean values, considered mask , treated such. if index non-boolean values, it's treated indexes values. makes perfect sense me.

3) not true. returned has same size of index, independent if it's matrix or vector. exception if index vector, output single row. idea indexing single vector/matrix returns of same size:

octave> = 4:7 =     4   5   6   7  octave> a([1 1]) ans =     4   4  octave> a([1 3]) ans =     4   6  octave> a([1 3; 3 1]) ans =     4   6    6   4 

4) max take dimension argument @ least in octave. 3.6.4 text of max:

for vector argument, return maximum value. matrix argument, return maximum value each column, row vector, or on dimension dim if defined, in case y should set empty matrix (it's ignored otherwise).

the rest applies said on intro. if supply matrix, think of each column dataset.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -