How can I solve randperm error with words?

2 visualizzazioni (ultimi 30 giorni)
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 Commenti
Stephen23
Stephen23 il 24 Mag 2019
Modificato: Stephen23 il 24 Mag 2019
"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 il 24 Mag 2019
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)

Accedi per commentare.

Risposta accettata

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

Accedi per commentare.

Più risposte (1)

KSSV
KSSV il 24 Mag 2019
Modificato: KSSV il 24 Mag 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 Commenti
KSSV
KSSV il 24 Mag 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,:)

Accedi per commentare.

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by