Max and Min values within vectors in a cell matrix

Hi everyone. I'm a strong user of MATLAB Answers and I've searched a lot to find an answer to this particular question but I couldn't... I need to find max and min values within vectors that are in a cell matrix. The cell matrix is quite big Nx865x351 where N will not be bigger than 10. Each cell has a vector (1xM, with M variable from vector to vector) or it can be an empty vector (so vectors in the different cells HAVE NOT the same size). So, for each 'N' I want to find the max and min value of all the values in the vectors contained in the 865x351 subpart of the cell matrix. I managed to do it in some way (written below) but I definitevely don't like it, I think there must be a faster and better way. At the end I just want 2 vectors with the max and min values for each N position. In the code
%pos_number=N
%power_critical_all -> cell matrix Nx865x351
min_interf=zeros(1,pos_number);
max_interf=zeros(1,pos_number);
mn = cellfun(@(x) min(x(:)),power_critical_all,'UniformOutput',0);
mx = cellfun(@(x) max(x(:)),power_critical_all,'UniformOutput',0);
% here I replace empty vectors by NaNs because cell2mat doesn't work then if I don't do it
mn(cellfun(@isempty,mn))={NaN};
mx(cellfun(@isempty,mx))={NaN};
mn=cell2mat(mn);
mx=cell2mat(mx);
for i=1:pos_number
min_interf(i)=min(mn(i,:));
max_interf(i)=max(mx(i,:));
end
An annex to this question (not so important but if someone knows how to...): what if I have the case that each vector (not an empty one, obviously) within a cell of the cell matrix has two rows and I just want to get the min and max values from the second row?
I hope I explain myself correctly and thanks in advance for your comments!

1 Comment

CELLFUN(@isempty) is much slower than CELLFUN('isempty').
All your cell elements are vectors. Then CELLFUN(@min) is about 4 times faster than CELLFUN(@(x) min(x(:)).

Sign in to comment.

 Accepted Answer

variant
N = size(C,1);
out = zeros(N,2);
for i1 = 1:N
p1 = [C{i1,:,:}];
out(i1,:) = [min(p1) max(p1)];
end

8 Comments

"[C{i1,:,:}]" will work, because Juan stated: "Each cell has a vector (1xM, with M variable from vector to vector)".
Unfortunatly MATLABs HORZCAT function (called for "[...]") does not pre-allocate the output sufficiently. But the C-Mex http://www.mathworks.com/matlabcentral/fileexchange/28916-cell2vec does such that it can be twice as fast.
But HORZCAT is not the bottleneck of this method, because it needs to allocate a lot of temporary memory.
with the data I have now is working but I will have different data soon... do you think (with disregard of the computation time) I'll have a problem with the code provided by Andrei in some situations? The vectors in the cell matrix are not larger than 1x12 (that is M<=12 always). Thanks!
Hi Jan! Thank you very much for your helpful comment.
@Juan: Andrei's code is a good choice.
Some speed measurements:
Your original code: 8.75 sec
Your code with CELLFUN('isempty'): 7.87 sec
Your code with CELLFUN('isempty') and CELLFUN(@max): 3.01 sec
Andrei: 0.84 sec
Andrei with Cell2Vec C-Mex: 0.42 sec
Jan with MinMaxElem C-Mex: 0.60 sec
Interpretation: Using CELLFUN efficiently accelerates the program by a factor 3.
Although the MinMaxElem function does not need temporary memory, the data vectors with N<=12 are so tiny, that the overhead of calling a Mex is too time-consuming.
But the advantage of pre-allocation in CELL2VEC is impressive. Or to be more precise: It is disappointing, that such a basic function as MATLAB's HORZCAT is weakly implemented.
While the speedup of 8.75 to 0.84 will be important, it is questionable, if the installation of a compiler is useful to get further 0.42 seconds.
Excellent explanation and predisposition from you! I will take into account your solution for further developments, now I cannot use a compiler... Thanks a lot, man!
For 32 bit MATLAB, MacOS and Linux a compiler is always available, but not for 64-bit Windows systems. I curious if LCC64 will be bundled in the future.
Dear Andrei or Jan,
I'd like to add a question to this one. I'd need now to be able to get where the min/max value were found, that is in which cell of the 865x351 possible for each N.
I've been trying for a while but no luck...
I'd be grateful if you can give me an advice.
Thanks in advance!
Dear Andrei and Juan I have same problem, but in my case, vector is 2xM, cell = {[1:5 6:10],[10:15 16:20],[21:25 26:30]} how to find max and min value for each vector?? and the answer may be like this: max =[5 10 15 20 25 30] min=[1 6 10 16 21 26] thanks in advance and sorry for comment a new question

Sign in to comment.

More Answers (1)

I'm using a C-Mex for finding the Min/Max elements in a set of arrays: FEX: MinMaxElem :
% C is a {Nx865x351} cell.
minValue = zeros(1, N);
maxValue = zeros(1, N); % [EDITED]: was "minValue" a 2nd time
for i = 1:N
[minValue(i), maxValue(i)] = MinMaxElem(C{i, :, :});
end
Getting Min and Max at the same time is much faster than obtaining them separately: For the sequence [1, 2, 3] the first element is the Min and the Max, for the following elements it is enough to check, if it is the new Max - if so, a check for a new Min value can be omitted!

3 Comments

Hi Jan, thank you very much. I've never used a C-Mex so I have to read a little bit in order to make it work. I tried to run the code you provide me and I got some errors.
Please post the error messages, such that we can give an advice.
Using a C-Mex is not hard: Install a C-compiler (I struggled just for 2 days to install MSVC2010+SDK7.1+SP1, 64 bit. Actually it is trivial, but there are bugs in the SP1 installers provided by Microsoft.), run "mex -setup", download the FEX submission, run "mex -O MinMaxElem.c". Or you can download the pre-compiled DLL from my webpage, but this works for 32 bit only at the moment.
The error was just because I don't have a compiler installed in this laptop (of my work place) and I cannot install anything here.
Since other persons will use the code I'm doing now, I should follow Andrei's advice which is simpler and doesn't use external files and it's way faster than my code.
Anyway thanks a lot for your help Jan!

Sign in to comment.

Categories

Find more on Debugging and Improving Code in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!