How can I solve randperm error with words?

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 comentarios

You forgot to keep the middle line that Stephen gave you:
X = nchoosek(1:numel(Tile),2);
Martha Spadaccino
Martha Spadaccino el 24 de Mayo de 2019
I want a random order..
Stephen23
Stephen23 el 24 de Mayo de 2019
Editada: Stephen23 el 24 de Mayo de 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 el 24 de Mayo de 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)

Iniciar sesión para comentar.

 Respuesta aceptada

madhan ravi
madhan ravi el 24 de Mayo de 2019
Editada: madhan ravi el 24 de Mayo de 2019
[x,y]=ndgrid(Tile);
xy=[x(:),y(:)];
xy(~strcmp(xy(:,1),xy(:,2)),:)

6 comentarios

Martha Spadaccino
Martha Spadaccino el 24 de Mayo de 2019
@ madhan ravi
This code generate the error: "Undefined function 'full' for input arguments of type 'cell'"
Error in ndgrid x = full(varargin{j(1)}(:));
madhan ravi
madhan ravi el 24 de Mayo de 2019
I didn’t get any error.
Martha Spadaccino
Martha Spadaccino el 24 de Mayo de 2019
What Matlab version do you use? I have R2016b
madhan ravi
madhan ravi el 24 de Mayo de 2019
Editada: madhan ravi el 24 de Mayo de 2019
https://in.mathworks.com/help/matlab/ref/strcmp.html#btwfvmr_seealso strcmp() was introduced before 2006a perhaps, I think that’s not an issue. Perhaps they started dealing with cells after 2016a , I couldn’t find any information in release notes at the moment.
Stephen23
Stephen23 el 24 de Mayo de 2019
Editada: Stephen23 el 24 de Mayo de 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 el 24 de Mayo de 2019
Editada: madhan ravi el 24 de Mayo de 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.

Iniciar sesión para comentar.

Más respuestas (1)

KSSV
KSSV el 24 de Mayo de 2019
Editada: KSSV el 24 de Mayo de 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 comentarios

Martha Spadaccino
Martha Spadaccino el 24 de Mayo de 2019
If I run this code I get nothing..
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,:)
Martha Spadaccino
Martha Spadaccino el 24 de Mayo de 2019
It's right! Thank you!

Iniciar sesión para comentar.

Categorías

Productos

Preguntada:

el 24 de Mayo de 2019

Editada:

el 24 de Mayo de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by