How to find max and min values from the cell(x*1 matrix) array elements !!
    5 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
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
  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.
Réponses (2)
  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)]
  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
      
      
 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.
Voir également
Catégories
				En savoir plus sur Matrix Indexing dans Help Center et File Exchange
			
	Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


