I have a cell array in MATLAB which contains some matrices in a descending size order (e.g. [512x512 double] [256x256 double] [128x128 double] etc).
Now I want to find and wipe (turn into zeros) the 100 smallest elements in the matrices in the cell array altogether (not in each matrix individually). For example, the smallest element might be in the 128x128 matrix, the next in the 512x512 matrix and so on.
How can I do it most efficiently?
The next is my super-slow code for the case of three such cell arrays - H,D,V (percent2zero
is a parameter for the percent of elements to wipe from the whole cell array):
details=[H;D;V];
for k=1:3
numOfLevels=length(details(k,:));
TotalCoeffs=0;
minimalInEachLevel=zeros(1,numOfLevels);
for i=1:numOfLevels
temp=cell2mat(details(k,i));
TotalCoeffs=TotalCoeffs+numel(temp);
minimalInEachLevel(i) = min(min(abs(temp)));
end
CoeffsToBeRemoved=percent2zero*TotalCoeffs/100;
for i=1:CoeffsToBeRemoved
for j=1:numOfLevels
temp=cell2mat(details(k,j));
minimalInEachLevel(j) = min(min(abs(temp)));
end
[val,ind]=min(minimalInEachLevel);
temp=cell2mat(details(k,ind));
temp(find(abs(temp==val),1))=0;
details(k,ind)=mat2cell(temp,size(temp,1),size(temp,2));
end
end
cellfun
- Adriaan