Find maximum value in a column of each cell in a large set of cell array?
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Raju Kumar
el 8 de Mzo. de 2023
Comentada: Fangjun Jiang
el 8 de Mzo. de 2023
I have a large cell array (e.g., 35598x1 cell). Each cell consists of mxn double (e.g., 26x5 double). I would like to find maximum value in nth column of each cell (let's say 5th column). How do I do that? Is it possible to do without using a loop as it is taking so much of time. Any help will be highly appriciated. Thank you.
3 comentarios
Dyuman Joshi
el 8 de Mzo. de 2023
Loops if used properly can be very efficient.
What have you tried? Show us your code and attach the data using the paperclip button.
Respuesta aceptada
Más respuestas (1)
Dyuman Joshi
el 8 de Mzo. de 2023
Editada: Dyuman Joshi
el 8 de Mzo. de 2023
Preallocate data accordingly for outputs of big size -
load Data.mat
f1 = @() loopprealloc(alpha200plus);
f2 = @() simpleloop(alpha200plus);
f3 = @() funcell(alpha200plus);
%checking if outputs are equal or not
isequal(f1(),f2(),f3())
fprintf('time taken by loop with preallocation = %f secs', timeit(f1))
fprintf('time taken by loop WITHOUT preallocation = %f secs', timeit(f2))
fprintf('time taken by cellfun = %f secs', timeit(f3))
function y = loopprealloc(x)
%Preallocation
y=zeros(size(x));
for k=1:size(x,1)
y(k,1) = max(x{k,1}(:,5));
end
end
function y = simpleloop(x)
for k=1:size(x,1)
y(k,1) = max(x{k,1}(:,5));
end
end
function y = funcell(x)
y = cellfun(@(in) max(in(:,5)), x);
end
1 comentario
Fangjun Jiang
el 8 de Mzo. de 2023
Nice job putting all three apporaches together so quickly and concise.
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!