Assigning empty matrix to empty sub-matrix in Matlab -
when running next code in matlab:
a = magic(3); b = []; a([],:) = [] % works a([],:) = b % doesn't work i error when using variable 'b':
>> tmp = 8 1 6 3 5 7 4 9 2 subscripted assignment dimension mismatch. error in tmp (line 5) a([],:) = b does know what's going on here? why 1 assignment works , other doesn't?
here's guess @ why matlab behaves that:
i think assigning [] can considered special operator, i.e. a(1,:) = [] delete first row of a. though size(a(1,:)) differs size([]), reckon matlab interpreter knows special case not assignment.
however a(1,:) = b when b=[] give subscript dimension mismatch. think right behaviour because in case assigning, , you're trying assign 0-by-0 1-by-3 dimension mismatch. same goes a([],:) = b, you're trying fit 0-by-0 0-by-3 space 1 time again mismatch.
so in conclusion, sec case assignment operator , error makes sense. first case special delete operator , hence no error.
i have no references of (this find in docs doesn't cover everything)
however don't think explains behaviour, examples brought comments:
assume:
a = magic(3); a2 = magic(4); b = []; a([],:) + a2([],:) gives dimension mismatch error expected. a([],:) = a2([],:) not throw error... me not expected a([],:) = b(:) not throw error... 1 time again quite strange, unless can assume (:) operation returns comma separated list {:} (although know not case)??? these cases seem inconsistent me.
we can expand on case 2:
a([],:) = zeros(0,0) a([],:) = zeros(0,2) a([],:) = zeros(0,3) a([],:) = zeros(0,4) only first case throws error , other 3 accepted matlab. looks reply creating farther questions :/
matlab matrix
No comments:
Post a Comment