MATLAB Answers

How to find max and min values from the cell(x*1 matrix) array elements !!

8 views (last 30 days)
Ram
Ram on 19 Jul 2018
Edited: Ram on 23 Jul 2018
hallo everyone, lets say, cell array have 600*1 matrix in that each cell consists of the X*1 matrix. now i want to find a max and min values. i tried to use min and max functions but these are not useful for cell arrays. so i try to find without inbulit functions. this is my code:-
d_max = d_main(1,1);
d_min= d_main(1,1);
for m = 1:length(mydata)
d_main = mydata{m,2};
for q = 1:length(d_main(1:end,1))
if d_main(q,1) > d_max
d_max = d_main(q,1);
elseif d_main(q,1)< d_min
d_min = d_main(q,1);
end
end
mydata{m,3} = d_max;
mydata{m,4} = d_min;
end
here, the problem is i am getting few rows are correct max numbers and remaining all are wrong. and all min values are wrong. i tried max and min function in command window.
  3 Comments
Jan
Jan on 19 Jul 2018
My note was just a general hint. size(d_main, 1) is nicer and more efficient than length(d_main(1:end,1)), which creates a temporary vector 1:end only to create the temporary vector d_main(1:end,1) only to measure its length. <size< can solve this directly.

Sign in to comment.

Answers (2)

KSSV
KSSV on 19 Jul 2018
%%make some random data
N = 5 ;
C = cell(N,1) ;
for i = 1:N
C{i} = rand(randperm(100,1),1) ;
end
% GEt min and max
themin = zeros(N,1) ; themax = zeros(N,1) ;
for i = 1:N
T = C{i} ;
ma = T(1); mi = T(1);
for j = 2:length(T)
if ma < T(j), ma = T(j);
elseif mi > T(j), mi = T(j);
end
end
themin(i) = mi ; themax(i) = ma ;
end
%%C0mpare
[themin cellfun(@min,C)]
[themax cellfun(@max,C)]

Jan
Jan on 19 Jul 2018
Edited: Jan on 19 Jul 2018
The problem is, that you do not reset the minimal and maximal values inside the loop. An improved version:
for m = 1:length(mydata)
d_main = mydata{m,2};
% Now ATFER d_main is copied from mydata, not before the loop:
d_max = d_main(1); % <-- move this INSIDE the loop
d_min = d_main(1); % <-- move this INSIDE the loop
for q = 2:size(d_main, 1) % You can start at 2
if d_main(q) > d_max
d_max = d_main(q);
elseif d_main(q)< d_min
d_min = d_main(q);
end
end
mydata{m,3} = d_max;
mydata{m,4} = d_min;
end
But KSSV's method is much better:
mydata(:, 3) = cellfun(@max, mydata(:,2), 'UniformOutput', false);
mydata(:, 4) = cellfun(@min, mydata(:,2), 'UniformOutput', false);
Or:
data2 = cat(2, data{:, 2});
mydata(:, 3) = num2cell(max(data2, [], 2))
mydata(:, 4) = num2cell(min(data2, [], 2))
  5 Comments
Ram
Ram on 23 Jul 2018
@Jan. actually i want to find index's of each max and min values. After this index values should be compare to before derivative my cell matrix(2*2 matrix in each cell). for example, i have max value is 5 which has index is 2 (from d_main). later, i am finding index 2 value in intial matrix(2*2 =x*y matrix lets say). when i compare this index value i got two values in that matrix(x*y) which has 10,11. but here i extract only x(10, index is 2) values. similarly, for all min values.
my new code is,
for m = 1:length(mydata)
[M,I]=max(mydata{m, 2});
mydata{m,3} = [M,I];
similarly for min values.
comparing index values but getting error!
for w =1:length(mydata)
index1 = mydata{w,3}(:,2)==mydata{w,1}(:,1);
index2 = mydata{w,4}(:,2)==mydata{w,1}(:,1);
mydata{w,1}(:,1) = mydata{w,5};
mydata{w,1}(:,1) = mydata{w,6};
end
how can i compare these index values to another cell matrix index and extract those first coulmn value only in each cell matrix.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!