Create cell arrays from doubles

2 vues (au cours des 30 derniers jours)
gsourop
gsourop le 4 Mar 2017
Commenté : Greg Heath le 5 Mar 2017
Hi everyone,
I would like to ask how I can combine 3 doubles into one cell array. I want to get a cell array 2x1, say abc_i, where the firt cell will retrieve the values from a(1,1:t) b(1,1:t) and c(1,1:t). The second cell array should contain the respective values for a(2,1:t) b(2,1:t) and c(2,1:t). The code below delivers a cell array 2x1 but both cell contain the same values, retrieved from a(2,1:t) b(2,1:t) and c(2,1:t).
aaa = cell(2,1);
bbb = cell(2,1);
ccc = cell(2,1);
aaa = cellfun(@(x) randn(5,2), aaa, 'uni', false);
bbb = cellfun(@(x) randn(5,2), bbb, 'uni', false);
ccc = cellfun(@(x) randn(5,2), ccc, 'uni', false);
for t=1:5;
for i=1:2
a(i,t)=mean(aaa{i}(t,:),2);
b(i,t)=mean(bbb{i}(t,:),2);
c(i,t)=mean([aaa{i}(t,:) bbb{i}(t,:)],2);
abc_i = cell(2,1);
abc_i = cellfun(@(x) [a(i,1:t); b(i,1:t); c(i,1:t)],abc_i , 'uni', false);
end
end
Any help would be much appreciated. Thanks in advance.
  5 commentaires
gsourop
gsourop le 4 Mar 2017
I am using neural networks and i are the number of hidden unit, ranging for 1:20 (for simplicity in the exercise above I used 2). I have j=90 predictors and produce the forecasts for 2000 observations (for simplicity I used 5). Hence, I created a 3 cell arrays of 30 predictors (because they can be divided like this according to the theory ; in the exercise above denoted as aaa,bbb,ccc) . Each cell array has the form {i}(t,j). I want to create 3 additional predictor, extracted from the mean of predictors for every t and every i (a,b and c). Then, I want to combine these means into a single cell array (abc_i). The reason I want to do this is because otherwise the code will be full of doubles. Is it clear now? I apologize for my English but I am not a native speaker.
Greg Heath
Greg Heath le 5 Mar 2017
Go with the code full of doubles.
Greg

Connectez-vous pour commenter.

Réponses (1)

Guillaume
Guillaume le 4 Mar 2017
Same as Image Analyst, I think that you're probably needlessly complicating things and that you don't need cell arrays. For example, following your demo, aaa would be better stored as a 3D matrix:
aaa_m = cat(3, aaa{:});
it is then trivial to calculate your mean in one line without a single loop (there was never any point to your t loop in any case)
a = permute(mean(aaa_m, 2), [3 2 1])
The permute above is only there so that a is the same shape as in your demo. It's not actually needed if you're going to do further processing with a.
If you really want your a, b, c together in a cell array, you will first have to concatenate them into a 3D matrix, then split that matrix into a cell array. Again, there's no point in the cell array. Keeping it as a 3D matrix would be simpler:
abc_m = cat(3, a, b, c); %this is probably a lot more useful than the cell array
abc_i = squeeze(num2cell(permute(abc_m, [3 2 1]), [1 2]))
Again, the squeeze and permute is to get the arrays the same shape as you asked. There's little point in them.

Catégories

En savoir plus sur Matrix Indexing 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!

Translated by