Find minimum in groups defined by cell arrays

I have a cell array I and a vector X. Each item in I specifies a group of indices of items in X. I would like to find the minimum in each group.
The naive code which does the work is
M = zeros(size(I))
for i = 1:numel(I)
M(i) = min(X(I{i}));
end
Or alternatively
M = cellfun(@(idx) min(X(idx)), I);
Both approaches are not as fast as I would like. Is there any way to make it faster? For example, transform the input, somehow, into a valid input of `splitapply' or something similar?

3 comentarios

Questions for clarification:
(1) I think your cell array I and vector X looks like:
X = rand(4,1);
I = {[true;true;false;false],[false;false;true;true]};
Is my above understainding correct?
(2) Does each item in X belongs to only one group? or sometimes belongs to multiple groups like following?
X = rand(4,1);
I = {[true;true;true;false],[false;false;true;true]}; % 3rd item belongs to both group
Matt J
Matt J el 26 de Dic. de 2018
You had to have used a for-loop just to create I. Are you sure that's not going to bottleneck your code even if you did manage to accelerate the min operations?
Alexander Shtof
Alexander Shtof el 26 de Dic. de 2018
I contains indices (integers) and not logical vectors. Becides, the assumptions are correct.

Iniciar sesión para comentar.

Respuestas (1)

Matt J
Matt J el 26 de Dic. de 2018
Editada: Matt J el 26 de Dic. de 2018
Cell arrays aren't built for speed. Instead of keeping I as a cell array, therefore, it would be better to encode the same information in the columns of a sparse binary matrix,
for i=1:N
I{i}=indices(:).'; %computed somehow
J{i}=ones(1,numel(indices));
end
S=sparse([I{:}],[J{:}],true,numel(X), numel(I));
Now, you can do your computation as follows,
xmax=max(X(:));
M = xmax - max((xmax-X(:)).*S,[],1);

3 comentarios

Alexander Shtof
Alexander Shtof el 26 de Dic. de 2018
I cannot change the structure of I, since it is obtained from another MATLAB function which I cannot control - the `rangesearch' function.
Matt J
Matt J el 26 de Dic. de 2018
Editada: Matt J el 26 de Dic. de 2018
Well, you can still derive the sparse matrix, S, from I as above and do the min. computations as shown. If you are going to do multiple computations like this, then it may prove worth the investment to build S.
Image Analyst
Image Analyst el 26 de Dic. de 2018
Like Matt said, I bet it's that you are using cell arrays, not that you're using a for loop. For loops are fast now. I just did a for loop (doing nothing inside) of one hundred million iterations and it took 0.09 seconds. I don't know how many iterations you're doing or what kind of speed/times you're getting but I'm pretty sure it's not the for loop itself causing a slow program. I bet it's the cell array. But even cell arrays are not that slow unless there are lots of cells, like tens of thousands, not a few dozen. What times are you seeing?

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 25 de Dic. de 2018

Editada:

el 26 de Dic. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by