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!