Sat 22 Sep 2012 10:01:17 AM UTC, comment #3:
Incidentally, there are further differences in behaviour, for example the following in ML:
>> repmat(1, [2,2], 3)
ans(:,:,1) =
1.0000e+000 1.0000e+000
1.0000e+000 1.0000e+000
ans(:,:,2) =
1.0000e+000 1.0000e+000
1.0000e+000 1.0000e+000
ans(:,:,3) =
1.0000e+000 1.0000e+000
1.0000e+000 1.0000e+000
>> repmat(1, [2,2], 3, 3)
Error using repmat
Too many input arguments.
>> repmat(1, [2,2,2], 3)
ans(:,:,1,1) =
1.0000e+000 1.0000e+000
1.0000e+000 1.0000e+000
ans(:,:,2,1) =
1.0000e+000 1.0000e+000
1.0000e+000 1.0000e+000
ans(:,:,1,2) =
1.0000e+000 1.0000e+000
1.0000e+000 1.0000e+000
ans(:,:,2,2) =
1.0000e+000 1.0000e+000
1.0000e+000 1.0000e+000
ans(:,:,1,3) =
1.0000e+000 1.0000e+000
1.0000e+000 1.0000e+000
ans(:,:,2,3) =
1.0000e+000 1.0000e+000
1.0000e+000 1.0000e+000
>> repmat(1, [2,2,2], [3,3])
ans(:,:,1,1,1) =
1.0000e+000 1.0000e+000
1.0000e+000 1.0000e+000
ans(:,:,2,1,1) =
1.0000e+000 1.0000e+000
1.0000e+000 1.0000e+000
ans(:,:,1,2,1) =
1.0000e+000 1.0000e+000
1.0000e+000 1.0000e+000
ans(:,:,2,2,1) =
1.0000e+000 1.0000e+000
1.0000e+000 1.0000e+000
... lots more output
So it seems m and n need not be scalar in ML, the dimensions are in fact just tacked onto each other. This is consistent with what was seen with empty inputs. Therefore you could get rid of the error and just do something like:
# check second arg first so it will be one if both
# m and n are empty
if (isempty (n))
n = 1;
endif
if (isempty (m))
m = n;
n = 1;
endif
if isvector(m) && isvector(n)
idx = [m(:)',n(:)'];
else
error('M and N must be scalars or vectors')
endif
|
Sat 22 Sep 2012 09:35:00 AM UTC, comment #2:
I realised I made a mistake when I first reported this.
Repmat does not replace empty values with 1, it ignores them and treats the non-empty values as if they were the first argument:
>> repmat([1,2,3], [], 4)
ans =
1.0000e+000 2.0000e+000 3.0000e+000
1.0000e+000 2.0000e+000 3.0000e+000
1.0000e+000 2.0000e+000 3.0000e+000
1.0000e+000 2.0000e+000 3.0000e+000
>> repmat([1,2,3], 4, [])
ans =
1.0000e+000 2.0000e+000 3.0000e+000
1.0000e+000 2.0000e+000 3.0000e+000
1.0000e+000 2.0000e+000 3.0000e+000
1.0000e+000 2.0000e+000 3.0000e+000
so your patch is not quite right. Something like the following might work:
# check second arg first so it will be one if both
# m and n are empty
if (isempty (n))
n = 1;
endif
if (isempty (m))
m = n;
n = 1;
endif
|
Tue 18 Sep 2012 10:16:45 PM UTC, original submission:
In Matlab I get the following output from repmat:
>> repmat([1,2,3], [], 4)
ans =
1.0000e+000 2.0000e+000 3.0000e+000
1.0000e+000 2.0000e+000 3.0000e+000
1.0000e+000 2.0000e+000 3.0000e+000
1.0000e+000 2.0000e+000 3.0000e+000
>> repmat([1,2,3], 2, [])
ans =
1.0000e+000 2.0000e+000 3.0000e+000
1.0000e+000 2.0000e+000 3.0000e+000
>> repmat([1,2,3], [], [])
ans =
1.0000e+000 2.0000e+000 3.0000e+000
Empty arguments appear to be treated as 1. Octave does not permit this behaviour.
|