Thursday, 18 August 2016

Weird matlab behaviour for empty matrices.

I've noticed that (at least in Matlab 2015b) a weird behaviour with Matlab matrices.

Here are two ways of making different empty matrices:
>> a=[]
>> size(a)
ans=
    0  0
>> isempty(a)
ans=
    1


>> b=find(1==[0 0])
>> size(b)
ans=
    1  0
>> isempty(b)
ans=
    1


They both look empty, but have different sizes. But critically for some code, they behave very differently in a for loop
>> for ind=a
>>    ...this never gets executed!
>> end;

>> for ind=b
>>    ...this gets executed once, with ind=[];
>> end;

This is a pernicious problem. Is there any reason for it to be like this?

No comments:

Post a Comment