How to use min function so that it stops at the first minimum value in a column matrix, stores values, and then continues going until it finishes the entire matrix? (details below)
조회 수: 1 (최근 30일)
이전 댓글 표시
would like to write a loop where i would like to sort through a column vector and stop when it hits a min value, these values would get stored in a subsequent array, then it keeps going until it hits another min value, and then these values would get stored in a subsequent array, and so on until it reaches and finishes the entire column vector. How would I go about doing this?
댓글 수: 4
Rik
2021년 1월 9일
Is that minimum value guaranteed to be the same value? Or should the function determine a new minimum value?
답변 (2개)
Rik
2021년 1월 9일
편집: Rik
2021년 1월 9일
If the minimum value you're after is the same throughout the array:
data=[0.200 0.300 0.400 0.001 0.002 0.003 0.004 0.003 0.002 0.001];
ind=find(ismember(data,min(data)));
ind=[0 ind];
ind_start=ind(1:(end-1))+1;
ind_end= ind(2: end ) ;
output=arrayfun(@(i1,i2) data(i1:i2),ind_start,ind_end,'UniformOutput',false);
celldisp(output)
Alternatively, if the minimum can change over the array:
output={};ind1=0;
data=[0.200 0.300 0.400 0.0001 0.002 0.003 0.004 0.003 0.002 0.001];
% ^ added a 0
while ind1<numel(data)
ind1=ind1+1;
[~,ind2]=min(data(ind1:end));
ind2=ind1+ind2-1;
output{end+1}=data(ind1:ind2); %#ok<SAGROW>
ind1=ind2;
end
celldisp(output)
댓글 수: 0
Bruno Luong
2021년 1월 9일
편집: Bruno Luong
2021년 1월 9일
A=[0.200 0.300 0.400 0.001 0.002 0.003 0.004 0.003 0.002 0.001]
imin=find(A==min(A));
lgt=diff(union(imin,[0 length(A)]))
C=mat2cell(A,1,lgt);
Check (the min values are located at the end)
>> C{:}
ans =
0.2000 0.3000 0.4000 0.0010
ans =
0.0020 0.0030 0.0040 0.0030 0.0020 0.0010
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Line Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!