Azzera filtri
Azzera filtri

Randomization of columns of 3 cell arrays and accessing data

2 visualizzazioni (ultimi 30 giorni)
The main goal is to randomize the columns of 3 cell arrays. The names of the cell arrays are:
names = {'PairsEMG_walk','PairsEMG_up','PairsEMG_do'};
I have created an random index array Idx of length(#columnsPairsEMG_walk+#columnsPairsEMG_up+#columnsPairsEMG_do).
Its contents are values that relates to: integer = name of array and decimal = number of column of cell array I am tying to create a new cell array concatenating the randomize data using
condition = floor(Idx); %to point to the cell array column to extract the name of my cell array
column =100*rem(Idx,1); % to pint to the column of "that" cell array
For example, I have:
Idx = [ 2.0400 3.1200 1.1600 1.0900 2.0800 2.0200 3.1400 3.0200 3.0600 3.0100 1.1200 1.0200 2.1400 3.1500 1.1000 3.1300 2.1000 1.0400 2.1500 3.0900 2.0900 3.1100 2.1200 2.0100 1.0800 3.1000 1.0100 1.1400 3.0500 3.0400 1.0300 3.0800 1.0500 1.0600 1.0700 2.0500 2.1100 2.0600 1.1300 3.0700 2.0700 3.0300 2.1300 1.1100 2.0300 1.1500];
Therefore, the first value of condition is 2 and column 4, so I have to extract column 4 from PairsEMG_up and put it in EMGInputSeries, for the next iteration, I would need to extract column 12 from PairsEMG_do, and so on.
I tried to use eval(), but I am getting an error
for i=1:length(Idx)
EMGInputSeries{i}=eval([names(condition(i)),'{',column(i),'}'])
end
I get: Error using eval Argument must contain a character vector.
also tried:
eval(['names(',condition(i),'){',column(i),'}'])
and I get Error: The input character is not valid in MATLAB statements or expressions.
FYI, the PairEMG arrays are 3x15 cell array, and need to extract all rows of "that" column.
Thank you,

Risposta accettata

Stephen23
Stephen23 il 23 Giu 2018
Modificato: Stephen23 il 23 Giu 2018
eval is, like always, a red-herring that just distracts and misleads from finding real solutions. Beginners do this because they think that eval is a magic tool that will solve all of their problems, but then they get stuck trying to solve all of the pointless problems that eval causes. And so they persist in fighting eval instead of looking for simpler, neater, much more efficient, much less buggy solutions. Sigh.
"The main goal is to randomize the columns of 3 cell arrays"
Well that is easy, so lets do that:
C = {PairsEMG_walk,PairsEMG_up,PairsEMG_do};
for k = 1:numel(C)
C{k} = C{k}(:,randperm(size(C{k},2)));
end
To use your specific random column order index that you would like to use, we just replace the randperm operation with that index. Note that hiding indices in decimal fractions is.... innovative, but awkwardly complex:
idx = [2.0400 3.1200 1.1600 1.0900 2.0800 2.0200...]
ida = fix(idx);
idc = round(100*rem(idx,1)); % rounding avoids floating point problems
Personally I would keep all indices as whole numbers, which is easy by using two variables to start with (much simpler, easier to work with, no limit to the number of columns, more robust, and avoids any floating point issues entirely):
ida = [2, 3, 1, 1, 2, 2, ... ] % array index
idc = [4, 12, 16, 9, 8, 20, ... ] % column index
In any case, you can then use those two index vectors quite easily:
C = {PairsEMG_walk,PairsEMG_up,PairsEMG_do};
N = numel(ida);
D = cell(1,N);
for k = 1:N
D{k} = C{ida(k)}(:,idc(k));
end
There you are, the columns of the arrays within C are given in D in the random order that you have requested, and without using an evil eval anywhere. If that output is not what you want, then specify more precisely what you are actually trying to do and we will help you with that.
  2 Commenti
Stephen23
Stephen23 il 26 Giu 2018
Modificato: Stephen23 il 26 Giu 2018
@ErikaZ: your explanation is not clear to me. Is it required to have a totally random selection, or is it required to use the selection specified by Idx?
It would be easy to get the first ten ones, twos, and threes from Idx, or you could randomly select ten of each: which do you want? Is Idx even required at all for this selection, would it be possible to just randomly pick ten from each array?
Note that so far whether Idx is "random" or not is not relevant to its usage in my answer: the output is as random as Idx is. Is that what you want?
ErikaZ
ErikaZ il 27 Giu 2018
Thank you Stephen. I got it to work exactly how I needed it using your initial response as template. It might not follow the best coding techniques and commands but it is working.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by