Max and Min values within vectors in a cell matrix
12 views (last 30 days)
Show older comments
Juan Grosso
on 24 Aug 2011
Commented: Amalia adiba
on 16 Jan 2014
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
Jan
on 24 Aug 2011
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(:)).
Accepted Answer
Andrei Bobrov
on 24 Aug 2011
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
Amalia adiba
on 16 Jan 2014
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
More Answers (1)
Jan
on 24 Aug 2011
% 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
Jan
on 24 Aug 2011
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.
See Also
Categories
Find more on Performance and Memory 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!