How can I solve randperm error with words?
1 view (last 30 days)
Show older comments
Martha Spadaccino
on 24 May 2019
Edited: madhan ravi
on 24 May 2019
I had create this code
Tile = {'house', 'core', 'word', 'ask', 'question', 'horse', 'phone', 'eyes', 'hair', 'man'};
>> X = X(randperm(size(X,1)),:);
>> Error undefined function or variable X
I would have all possible random combinations pairs without repetitions and without have the same values (e.g horse - horse). How can I do?
4 Comments
Stephen23
on 24 May 2019
Edited: Stephen23
on 24 May 2019
"I want a random order.."
X = X(randperm(size(X,1)),:); % random row order.
Based on your question above, you are not runinng the complete code that i gave you. Exactly as Rik wrote, for some unknown reason you have decided to remove one lines of the code that I gave you. Of course it will not work if you remove a line of code!
This is the code I gave you, it does exactly as you requested (very efficiently!):
>> Tile = {'house', 'core', 'word', 'ask', 'question', 'horse', 'phone', 'eyes', 'hair', 'man'};
>> X = nchoosek(1:numel(Tile),2); % Why did you remove this?
>> X = X(randperm(size(X,1)),:);
>> Tile(X)
Accepted Answer
madhan ravi
on 24 May 2019
Edited: madhan ravi
on 24 May 2019
[x,y]=ndgrid(Tile);
xy=[x(:),y(:)];
xy(~strcmp(xy(:,1),xy(:,2)),:)
6 Comments
Stephen23
on 24 May 2019
Edited: Stephen23
on 24 May 2019
Neat use of ndgrid directly on the cell array!
Something to keep in mind: this ndgrid-based method generates much larger intermediate arrays than the final array, because the intermediate arrays include duplicate sets and repeated strings, before they simply get discarded. For small sets of strings this might not be a problem (for larger sets the nchoosek-based method used in my answer will use less memory).
Note that this answer does not return the rows in a random order, which the question requires: you will need to add randperm or similar.
@madhan ravi: apparently this error is due to an undocumented change in ndgrid. See:
https://www.mathworks.com/matlabcentral/answers/392649-generate-combinations-of-cells-that-contain-text#comment_707380
Does the installed ndgrid help mention anything about supporting strings or cell arrays ?
madhan ravi
on 24 May 2019
Edited: madhan ravi
on 24 May 2019
Thanks you Stephen, I definitely would have not answered if this question was already asked (as it seem to be). I am not going to remove the answer as Stephen’s comment is really valuable. This answer doesn’t meet the requirements as it was given as I was walking and gave the answer using my mobile which didn’t require enough brain efficiency.
Perhaps:
[x,y]=ndgrid(Tile);
xy=[x(:),y(:)];
filteredxy = xy(~strcmp(xy(:,1),xy(:,2)));
xy(randperm(size(filteredxy,1)),:) % random order
No Stephan help ndgrid doesn't mention anything about that.
More Answers (1)
KSSV
on 24 May 2019
Edited: KSSV
on 24 May 2019
Tile = {'house', 'core', 'word', 'ask', 'question', 'horse', 'phone', 'eyes', 'hair', 'man'};
% Gives random arrangement
X = Tile(randperm(length(Tile)));
% get all posibilities
idx = perms(1:length(Tile)) ;
iwant = Tile(idx) ;
3 Comments
KSSV
on 24 May 2019
YOu got nothing because the out put is terminated with ;
In the code iwant gives you all the possible permutations of the cell array tile.
Check:
iwant(1:10,:)
See Also
Categories
Find more on Whos in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!