randperm function for a table - keep rows together
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Alessandro Livi
el 10 de Jul. de 2024
I looked in help but everone seems to be randomizing a vector.
The closest ask I can find is
I have a table (including a category column) I asked (Q )about using Shuffle but if that doesn't handle table shuffling like I need i can take the performance hit and use randperm()
Here's what I'm doing:
app.TrialsTable = table('Size',[2*C 4],'VariableTypes',{'uint8','uint8','categorical','int16'});
... % fill the table from given input table, expanding rows as needed for number of repeats
app.TrialsTable(index, 1) = ... %and so on
...
app.TrialsTable = Shuffle(app.TrialsTable, ???); % if anyone knows how to do it this way
& or
app.TrialsTable(randperm(height(app.TrialsTable(:,:)))); % How do I make this line work??
% and then finally
set(app.RandTrialsTable,'Data',app.TrialsTable(:,:)); % save it to the UITable for display in the Figure
Testing at the command prompt for various options of randperm(...)
app.TrialsTable(randperm(height(app.TrialsTable(:,:))))
Error using ()
Subscripting into a table using one subscript (as in t(i)) is not supported. Specify
a row subscript and a variable subscript, as in t(rows,vars). To select variables,
use t(:,i) or for one variable t.(i). To select rows, use t(i,:).
I didn't use 1 subscript so the Error message doesn't help
Another try:
app.TrialsTable(:,:)(randperm(height(app.TrialsTable(:,:))))
Error: Invalid array indexing.
Test
(randperm(height(app.TrialsTable(:,:))))
ans =
Columns 1 through 14
17 18 13 8 3 9 19 27 14 6 25 15 2 5
Columns 15 through 28
7 23 24 12 22 20 28 21 26 4 16 1 10 11
I only have 4 columns (28 rows)
Try
(randperm(height(app.TrialsTable)))
ans =
Columns 1 through 14
27 14 22 4 10 2 21 3 5 15 18 28 23 7
Columns 15 through 28
24 16 25 9 6 13 26 17 19 20 1 11 8 12
No sign of the category column ('L' or 'R') sorting along with it.
OH! maybe I don't need to tell the height (as the Shuffle guy said)
(randperm(app.TrialsTable))
Error using randperm
Conversion to double from table is not possible.
try
(randperm(app.TrialsTable(:,:)))
Error using randperm
Conversion to double from table is not possible.
randperm(1:size(app.TrialsTable.Row))
ans =
1×0 empty double row vector
I tried on my own, time to ask for help!
1 comentario
Stephen23
el 10 de Jul. de 2024
Editada: Stephen23
el 10 de Jul. de 2024
"I didn't use 1 subscript so the Error message doesn't help"
Yes you did, exactly on the line where the error occured:
app.TrialsTable(randperm(height(app.TrialsTable(:,:))))
% ^ indexing using exactly one subscript ^
Lets split the code onto two lines to make it clearer:
X = randperm(height(app.TrialsTable(:,:)));
app.TrialsTable(X) % <- that is exactly one subscript
"How do I make this line work??"
You were so close: if you had paid attention to the error message then you might have gotten there yourself. Just generate the desired indices and then apply them to the rows:
T = array2table(magic(4))
X = randperm(height(T))
T(X,:) % two subscripts, not one
So your code would be something like:
X = randperm(height(app.TrialsTable));
app.TrialsTable = app.TrialsTable(X,:); % two subscripts, not one
The introductory tutorials explain fundamental MATLAB concepts, e.g. basic indexing:
Strongly recommended.
Respuesta aceptada
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!