How to store consequtive numbers and vector jumps in different matrices?

3 ビュー (過去 30 日間)
OK guys, lets say i have a matrix A=[1,2,3,6,7,8,11,12,13,14,15,17,18,19,30,31,32] , i want to locate the consequtive numbers and store them in matrices ,and also the the values that are not consequtive and store them in different matrices.So the output should be like:
A1=[1 2 3] B1=[3 6]
A2=[6 7 8 ] B2=[8 11]
A3 = [11 12 13 14 15 ] B3 = [15 17]
A4=[17 18 19 ] B4=[19 30]
A5=[ 30 31 32]
Any idea how can i solve this?

採用された回答

Riccardo Scorretti
Riccardo Scorretti 2022 年 3 月 25 日
編集済み: Riccardo Scorretti 2022 年 3 月 25 日
Hi. You can try this:
A = [1,2,3,6,7,8,11,12,13,14,15,17,18,19,30,31,32];
C = {} ; NC = {}; % Consecutive / Non Consecutive vectors
ind = find(diff(A)~=1);
for n = 1 : numel(ind), NC{n} = A(ind(n):ind(n)+1) ; end
ind = [1 ind+1 numel(A)+1];
for n = 1 : numel(ind)-1, C{n} = A(ind(n):ind(n+1)-1) ; end
C
C = 1×5 cell array
{[1 2 3]} {[6 7 8]} {[11 12 13 14 15]} {[17 18 19]} {[30 31 32]}
NC
NC = 1×4 cell array
{[3 6]} {[8 11]} {[15 17]} {[19 30]}
  4 件のコメント
Riccardo Scorretti
Riccardo Scorretti 2022 年 3 月 25 日
You mean, like that?
A = [1,2,3,6,7,8,11,12,13,14,15,17,18,19,30,31,32];
C = {} ; NC = {}; % Consecutive / Non Consecutive vectors
figure
ind = find(diff(A)~=1);
for n = 1 : numel(ind)
NC{n} = A(ind(n):ind(n)+1);
plot(ind(n):ind(n)+1, NC{n}, 'r') ; hold on;
end
ind = [1 ind+1 numel(A)+1];
for n = 1 : numel(ind)-1
C{n} = A(ind(n):ind(n+1)-1);
plot(ind(n):ind(n+1)-1, C{n}, 'b');
end
C
C = 1×5 cell array
{[1 2 3]} {[6 7 8]} {[11 12 13 14 15]} {[17 18 19]} {[30 31 32]}
NC
NC = 1×4 cell array
{[3 6]} {[8 11]} {[15 17]} {[19 30]}
PANAGIOTIS GEORGIOS ILIOPOUOS
PANAGIOTIS GEORGIOS ILIOPOUOS 2022 年 3 月 25 日
編集済み: PANAGIOTIS GEORGIOS ILIOPOUOS 2022 年 3 月 25 日
Yep nice thanks for answering .
What if i wanted to apply the same code in a datetime matrix?
For example
A = ['29-May-2017 00:02:00' '29-May-2017 00:03:00' '29-May-2017 00:04:00' '29-May-2017 00:08:00'...
'29-May-2017 00:09:00' '29-May-2017 00:010:00' '29-May-2017 00:11:00' '29-May-2017 00:14:00']
so as to find when the time values are non consequtive and also plot them like before.
i thinks its kinda trickier,but any comments would be helpfull.

サインインしてコメントする。

その他の回答 (1 件)

David Hill
David Hill 2022 年 3 月 25 日
A=[1,2,3,6,7,8,11,12,13,14,15,17,18,19,30,31,32];
a=diff(A);
n=num2str(a==1);nn=num2str(a~=1);
n=n(n~=' ');nn=nn(nn~=' ');
[s,e]=regexp(n,'[1]+');
[ss,ee]=regexp(nn,'[1]+');
for k=1:length(s)
AA{k}=A(s(k):e(k)+1);
end
for k=1:length(ss)
BB{k}=A(ss(k):ee(k)+1);
end
  2 件のコメント
PANAGIOTIS GEORGIOS ILIOPOUOS
PANAGIOTIS GEORGIOS ILIOPOUOS 2022 年 3 月 25 日
Works Great !!!
thanks for your help
PANAGIOTIS GEORGIOS ILIOPOUOS
PANAGIOTIS GEORGIOS ILIOPOUOS 2022 年 3 月 25 日
Do you know what else i need do add in this code, so as to store the order that each value has in the Matrix A ?
Because i also want to plot the consequtive/non consequtive values in the same figure but with different color,but in the right order.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by