# Find maximum value in a column of each cell in a large set of cell array?

10 Ansichten (letzte 30 Tage)
Raju Kumar am 8 Mär. 2023
Kommentiert: Fangjun Jiang am 8 Mär. 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 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Dyuman Joshi am 8 Mär. 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.
Raju Kumar am 8 Mär. 2023
@Dyuman Joshi @Fangjun Jiang Thanks for your reply. I have attached the file. Here is what I am trying
for i=1:size(alpha200plus) % File name is alpha200plus when Data.mat is read
AlphaToT(i,:) = max(alpha200plus{i,1}(:,5))
end
I actually access each cell and its data one by one, and then find a maximum. This takes so long to process 35598x1 cell (Please note, here the file size has been reduced to meet MATLAB upload size)
I also tried cellfun as,
cellfun(@max, alpha200plus, 'UniformOutput', false)
but this also gives a cell consiting a vector that has a maximum of existing variables inside.
Thanks.
Raju

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Fangjun Jiang am 8 Mär. 2023
c={rand(10),rand(11),rand(12)};
N=5;
cellfun(@(M) max(M(:,N)),c)
ans = 1×3
0.9427 0.9110 0.9603
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Raju Kumar am 8 Mär. 2023
Thank you very much @Fangjun Jiang

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (1)

Dyuman Joshi am 8 Mär. 2023
Bearbeitet: Dyuman Joshi am 8 Mär. 2023
Preallocate data accordingly for outputs of big size -
f1 = @() loopprealloc(alpha200plus);
f2 = @() simpleloop(alpha200plus);
f3 = @() funcell(alpha200plus);
%checking if outputs are equal or not
isequal(f1(),f2(),f3())
ans = logical
1
fprintf('time taken by loop with preallocation = %f secs', timeit(f1))
time taken by loop with preallocation = 0.018185 secs
fprintf('time taken by loop WITHOUT preallocation = %f secs', timeit(f2))
time taken by loop WITHOUT preallocation = 0.020928 secs
fprintf('time taken by cellfun = %f secs', timeit(f3))
time taken by cellfun = 0.036368 secs
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 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Fangjun Jiang am 8 Mär. 2023
Nice job putting all three apporaches together so quickly and concise.

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by