Reputation: 133
I am trying to shuffle a vector that is not yet defined
the vector consist of [0 0 0 ... 0 0 1 2 3 ... (n-zer)]
n
: length of vectorzer
: number of zeros at the beginninglike so:
PartZeroPartNum=@(zer,n) [zeros(1,zer),1:(n-zer)];
shuffled=@(zer,n) PartZeroPartNum(zer,n)(randperm(n));
it does not work as this part
PartZeroPartNum(zer,n)(randperm(n))
gives the error:
cannot call or index into temporary array
In contrast, it is working if I do it this way:
n=100;
PartZeroPartNum=logical([zeros(1,zer),1:(n-zer)]);
shuffled=@() PartZeroPartNum(randperm(n));
Is it possible to shuffle a more versatile vector as I tried to do above? maybe in another way?
The reason is that I need many examples of shuffled vectors so I thought to make this anonymous function first and then take samples easily like so:
ShVec= shuffled(50,100);
Upvotes: 0
Views: 101
Reputation: 30047
There are better ways to do this, but you could add yet another anonymous function
PartZeroPartNum=@(zer,n) [zeros(1,zer),1:(n-excited)];
fIndex = @(x,ii) x(ii);
shuffled=@(zer,n) fIndex(PartZeroPartNum(zer,n), randperm(n));
As mentioned in the comments, you would be better to use a function in its own m-file, this would be the most readable option as suggested by Cris.
Upvotes: 2
Reputation: 5735
The function call equivalence to A(index)
is subsref(A, struct('type', '()', 'subs', {index}))
see here.
So you can make your anonymous function
PartZeroPartNum=@(zer,n) [zeros(1,zer),1:(n-excited)];
shuffled=@(zer,n) subsref(PartZeroPartNum(zer,n), struct('type', '()', 'subs', {randperm(n)});
But I wouldn't do it as it is not very readable.
Upvotes: 1