How can I solve randperm error with words?

1 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2019-5-24
编辑:Stephen23 2019-5-24
"I want a random order.."
What do you think this line of my answer is for?:
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)
Martha Spadaccino
Martha Spadaccino 2019-5-24
Thank you so much! I definitively resolved the problem.
>> Tile = {'house', 'core', 'word', 'ask', 'question', 'horse', 'phone', 'eyes', 'hair', 'man'};
>> X = nchoosek(1:numel(Tile),2); % Why did you remove this? I thought to remove it..sorry!
>> X = X(randperm(size(X,1)),:);
>> Tile(X)

请先登录,再进行评论。

采纳的回答

madhan ravi
madhan ravi 2019-5-24
编辑:madhan ravi 2019-5-24
[x,y]=ndgrid(Tile);
xy=[x(:),y(:)];
xy(~strcmp(xy(:,1),xy(:,2)),:)
  6 个评论
Stephen23
Stephen23 2019-5-24
编辑:Stephen23 2019-5-24
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:
Does the installed ndgrid help mention anything about supporting strings or cell arrays ?
madhan ravi
madhan ravi 2019-5-24
编辑:madhan ravi 2019-5-24
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.

请先登录,再进行评论。

更多回答(1 个)

KSSV
KSSV 2019-5-24
编辑:KSSV 2019-5-24
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 个评论
KSSV
KSSV 2019-5-24
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,:)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by