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)

4 vues (au cours des 30 derniers jours)
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 commentaires
Sai Pamula
Sai Pamula le 9 Jan 2021
any ideas on how to go about solving this? I just need it in some way so I can easily plot all of these arrays on a graph
Rik
Rik le 9 Jan 2021
Is that minimum value guaranteed to be the same value? Or should the function determine a new minimum value?

Connectez-vous pour commenter.

Réponses (2)

Rik
Rik le 9 Jan 2021
Modifié(e) : Rik le 9 Jan 2021
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)
output{1} = 0.2000 0.3000 0.4000 0.0010 output{2} = 0.0020 0.0030 0.0040 0.0030 0.0020 0.0010
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)
output{1} = 0.2000 0.3000 0.4000 0.0001 output{2} = 0.0020 0.0030 0.0040 0.0030 0.0020 0.0010

Bruno Luong
Bruno Luong le 9 Jan 2021
Modifié(e) : Bruno Luong le 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

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by