Create cell arrays from doubles

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 comentarios

Image Analyst
Image Analyst el 4 de Mzo. de 2017
Why on earth would you want to do that? Why use more memory and complicate your life with cell arrays when you don't have to?
gsourop
gsourop el 4 de Mzo. de 2017
How else could I do it? In addition, mind that the numbers are greater than 2 and 5 and also I need a compact way to process further my results.
Cell arrays are NOT compact. For large arrays they use far, far more memory than a regular double array. I'm not sure what you want to do because you removed, or never added, comments. If you want a 3-D array with all matrixes in it, make one with cat:
array3d = cat(aaa, bbb, ccc);
I'm not sure what you're doing with the means. Please explain in English. Is it the mean of the arrays, a mean of rows or columns, or a local/sliding mean, or something else.
What is the purpose of a, b, c, and abc_i?
gsourop
gsourop el 4 de Mzo. de 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 el 5 de Mzo. de 2017
Go with the code full of doubles.
Greg

Iniciar sesión para comentar.

Respuestas (1)

Guillaume
Guillaume el 4 de Mzo. de 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.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 4 de Mzo. de 2017

Comentada:

el 5 de Mzo. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by