how to reduce time grabbing values from indices of cell arrays?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
lightroman
le 7 Nov 2017
Commenté : Andrei Bobrov
le 7 Nov 2017
I currently do the operation below and wanted to try to do this without a loop since n is very large.
value = 1:5;
results = zeros(n, 5);
for i = 1:n
idx = ismember(tag{i}, value);
results(i, 1:5) = (data{i}(idx))';
end
n is the number of 1xn cell arrays in the multidimensional one.
value can only be a value that exists in all n tag arrays. In this ex it must be 1 through 5.
results is a nx5 matrix of all the data associated with the value found in tag{n}.
tag and data are both multi dimensional cell arrays where size{tag{n}} == size(data{n}), for example:
tag{1} = {1; 2; 3; 4; 5} data{1} = {34; 42; 43; 52; 1}
tag{2} = {1; 2; 3; 4; 5; 6; 7; 8} data{2} = {32; 09; 12; 33; 4; 98; 67; 11}
tag{3} = {1; 2; 3; 4; 5; 6} data{3} = {98; 28; 48; 29; 9; 22}
tag{n} = {1; 2; 3; 4; 5 ...} data{n} = {x1; x2; x3; x4; x5 ...}
results = [34 42 43 52 1;
32 09 12 33 4;
98 28 48 29 9]
Can this be done with cellfun or similar and save time?
0 commentaires
Réponse acceptée
Andrei Bobrov
le 7 Nov 2017
d = [data{:}];
out = reshape(d(ismember([tag{:}],1:5)),5,[])';
4 commentaires
Andrei Bobrov
le 7 Nov 2017
tag{1} = {1; 2; 3; 4; 5}; data{1} = {34; 42; 43; 52; 1};
tag{2} = {1; 2; 3; 4; 5; 6; 7; 8}; data{2} = {32; 09; 12; 33; 4; 98; 67; 11};
tag{3} = {1; 2; 3; 4; 5; 6}; data{3} = {98; 28; 48; 29; 9; 22};
t = cat(1,tag{:});
t = [t{:}];
d = cat(1,data{:});
d = [d{:}];
out = reshape(d(ismember(t,1:5)),5,[])'
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Cell Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!