Create cell arrays from doubles
Show older comments
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 Comments
Image Analyst
on 4 Mar 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
on 4 Mar 2017
Image Analyst
on 4 Mar 2017
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
on 4 Mar 2017
Greg Heath
on 5 Mar 2017
Go with the code full of doubles.
Greg
Answers (1)
Guillaume
on 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.
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!