简体   繁体   中英

Why does Octave print “dimensions mismatch” whereas MATLAB does not?

I am trying to run a MATLAB code in Octave but got stuck at the following point:

I is an empty matrix, dimensions 0x4,

a = 2;

The command, belonging to a for-loop, is:

I = [I a];

MATLAB output: I = 2

Octave output: "horizontal dimensions mismatch (0x4 vs 1x1)"

I have found a way to work around this error but I would also like to understand: Why does MATLAB accept those different dimensions whereas Octave prints an error? Is there a different definition regarding empty matrices and extending those? (Specially because it is not a "normal" empty matrix but a 0x4 empty matrix?)

Matlab issues a warning, alerting you to the fact that this will become an error in future releases:

>> I = magic(4);
>> I(1:4,:) = []
I =
   Empty matrix: 0-by-4
>> [I 2]
Warning: This concatenation operation includes an empty array with an incorrect number of rows.
Concatenation including empty arrays will require all arrays to have the same number of rows in a future release. 

ans =
 2

Same code on Octave:

>> I = magic(4);
>> I(1:4,:)=[]
I = [](0x4)

>> [I 2]
error: horizontal dimensions mismatch (0x4 vs 1x1)

So essentially it's the same issue, except Matlab allows it with a warning for the time being, and is being slightly more informative as to which dimension is actually at fault here, whereas octave is stricter about it and hopes you figure out what it meant 😛. But in essence the behaviour is the same.

It is also very reasonable behaviour, since attempting to concatenate two matrices of different sizes / dimensions is more likely to have come from a bug rather than intended behaviour, even if one of the arrays has become empty in the process, so matlab is wise to go down the octave path here (so to speak).


PS. Note that in this scenario, something like [I;2 2 2 2] is perfectly valid and correct code on both interpreters: ie you're concatenating vertically a 4-column matrix with one row to a 4-column matrix with no rows, hence the number of columns is consistent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM