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

2 vues (au cours des 30 derniers jours)
Ram
Ram le 19 Juil 2018
Modifié(e) : Ram le 23 Juil 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 commentaires
Ram
Ram le 19 Juil 2018
Modifié(e) : Ram le 19 Juil 2018
@jan thanks for your reply. Yes, d_main is used before in my code. it has the derivative values which has 1000*1 matrix and cells are 600*1. i want to find max and min from those derivative values for each cell. when you observe it has -inf to +inf range. when i replaced to size(d_main,1) also getting incorrect values. for example.-
max(mydata{415, 1} )
ans =
0.2773
but appearing ans is 0.4364
Jan
Jan le 19 Juil 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.

Connectez-vous pour commenter.

Réponses (2)

KSSV
KSSV le 19 Juil 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)]
  2 commentaires
Ram
Ram le 19 Juil 2018
@KSSV thanks alot for your help. i got correct output.
Ram
Ram le 23 Juil 2018
@KSSV, do you have any idea... i am getting error, comment thanks in advance

Connectez-vous pour commenter.


Jan
Jan le 19 Juil 2018
Modifié(e) : Jan le 19 Juil 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 commentaires
Jan
Jan le 20 Juil 2018
@Ram: Sorry, I still do not understand it. Again: Please provide a small example for the input data and the wanted outputs. Use 2x1 cell and 3x2 arrays to keep it small.
Ram
Ram le 23 Juil 2018
Modifié(e) : Ram le 23 Juil 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.

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by