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)
Show older comments
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 Comments
SHIVAM KUMAR
on 9 Jan 2021
So do you want to find the minimum value of the array ?
Sai Pamula
on 9 Jan 2021
Sai Pamula
on 9 Jan 2021
Rik
on 9 Jan 2021
Is that minimum value guaranteed to be the same value? Or should the function determine a new minimum value?
Answers (2)
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)
Bruno Luong
on 9 Jan 2021
Edited: Bruno Luong
on 9 Jan 2021
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
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!