Fast calculation of min for a cell array?

2 views (last 30 days)
Sim
Sim on 10 Nov 2020
Commented: Sim on 10 Nov 2020
Hi, I have this cell array:
A =
3×160 cell array
Columns 1 through 5
{18×10754 double} {18×10754 double} {18×10754 double} {18×10754 double} {18×10754 double}
{31×14797 double} {31×14797 double} {31×14797 double} {31×14797 double} {31×14797 double}
{65×34310 double} {65×34310 double} {65×34310 double} {65×34310 double} {65×34310 double}
and for each of my 160 cells in A(3,:) (i.e. the 160 cells in the third row of A):
A(3,:) =
1×160 cell array
Columns 1 through 5
{65×34310 double} {65×34310 double} {65×34310 double} {65×34310 double} {65×34310 double}
I would need a very fast way to get the min of each column, and therefore such a cell array:
Amin =
1×160 cell array
Columns 1 through 5
{1×34310 double} {1×34310 double} {1×34310 double} {1×34310 double} {1×34310 double}
My attempt is the following:
t1 = tic();
for i = 1 : size(A(3,:),2)
[A_min_value, A_min_row] = min(A{3,i,:},[],1); % (min each column)
Amin_value{3,i,:} = A_min_value;
Amin_row{3,i,:} = A_min_row;
end
dt = toc(t1)
tic-toc time
dt = 0.129487033
Any faster way?

Accepted Answer

Stephen
Stephen on 10 Nov 2020
nc = size(A,2);
Amin_value = cell(1,nc);
Amin_row = cell(1,nc);
for k = 1:nc
[Amin_value{1,k},Amin_row{1,k}] = min(A{3,k},[],1);
end
  1 Comment
Sim
Sim on 10 Nov 2020
slightly faster and more compact! Thanks a lot :)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!