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 个评论
Sai Pamula
Sai Pamula 2021-1-9
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

请先登录,再进行评论。

回答(2 个)

Rik
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)
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 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

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by