Azzera filtri
Azzera filtri

randperm function for a table - keep rows together

24 visualizzazioni (ultimi 30 giorni)
Alessandro Livi
Alessandro Livi il 10 Lug 2024 alle 19:06
Modificato: Stephen23 il 10 Lug 2024 alle 21:37
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 Commento
Stephen23
Stephen23 il 10 Lug 2024 alle 21:25
Modificato: Stephen23 il 10 Lug 2024 alle 21:37
"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))
T = 4x4 table
Var1 Var2 Var3 Var4 ____ ____ ____ ____ 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
X = randperm(height(T))
X = 1x4
3 4 2 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
T(X,:) % two subscripts, not one
ans = 4x4 table
Var1 Var2 Var3 Var4 ____ ____ ____ ____ 9 7 6 12 4 14 15 1 5 11 10 8 16 2 3 13
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.

Accedi per commentare.

Risposta accettata

Alessandro Livi
Alessandro Livi il 10 Lug 2024 alle 19:22
Google helped where MatLab help couldn't
LMGTFY: "shuffle a matrix rows"
Very nice explanation. I'll be checking tutorialspoint in the future before asking here.
Cheers

Più risposte (0)

Prodotti


Release

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by