Azzera filtri
Azzera filtri

Extracting name of variables from cell array

2 visualizzazioni (ultimi 30 giorni)
Stephen
Stephen il 27 Apr 2016
Risposto: Kevin il 27 Apr 2016
Dear All,
I have a cell array combin which contains all possible combinations of 4 elements ('A';'B';'C';'D') taken 3 at a time.
combin =
'A' 'B' 'C'
'A' 'B' 'D'
'A' 'C' 'D'
'B' 'C' 'D'
coef = ['A';'B';'C';'D'];
coef = cellstr(coef);
combin = nchoosek(coef,3)
Each letter is also the variable name of a random number vector:
A = rand(10,1); B = rand(10,1); C = rand(10,1); D = rand(10,1);
I would like to obtain a matrix made of vectors A,B,C or D for each row of combin. For instance,
mod1 = [A B C];
...
mod4 = [B C D];
Any help would be highly appreciated.
Best, S.

Risposta accettata

Kevin
Kevin il 27 Apr 2016
It will be much easier if we can turn your 'A', 'B', 'C', 'D' into 1, 2, 3, 4. For example,
>> coef = [1;2;3;4];
>> combin = nchoosek(coef, 3)
combin =
1 2 3
1 2 4
1 3 4
2 3 4
Also assume that we can put vectors {A, B, C, D} into one big matrix (say R). For example,
>> R = rand(10,4)
R =
0.8147 0.1576 0.6557 0.7060
0.9058 0.9706 0.0357 0.0318
0.1270 0.9572 0.8491 0.2769
0.9134 0.4854 0.9340 0.0462
0.6324 0.8003 0.6787 0.0971
0.0975 0.1419 0.7577 0.8235
0.2785 0.4218 0.7431 0.6948
0.5469 0.9157 0.3922 0.3171
0.9575 0.7922 0.6555 0.9502
0.9649 0.9595 0.1712 0.0344
Then the following one line will create vector mod1,
>> mod1 = R(:, combin(1,:))
mod1 =
0.8147 0.1576 0.6557
0.9058 0.9706 0.0357
0.1270 0.9572 0.8491
0.9134 0.4854 0.9340
0.6324 0.8003 0.6787
0.0975 0.1419 0.7577
0.2785 0.4218 0.7431
0.5469 0.9157 0.3922
0.9575 0.7922 0.6555
0.9649 0.9595 0.1712

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by