Create cell arrays from doubles
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
Respuestas (1)
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.
0 comentarios
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!