How to find max and min values from the cell(x*1 matrix) array elements !!
    조회 수: 4 (최근 30일)
  
       이전 댓글 표시
    
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
  Jan
      
      
 2018년 7월 19일
				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.
답변 (2개)
  KSSV
      
      
 2018년 7월 19일
        %%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
      
      
 2018년 7월 19일
        
      편집: Jan
      
      
 2018년 7월 19일
  
      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
  Jan
      
      
 2018년 7월 20일
				@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.
참고 항목
카테고리
				Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!