I want to sort a data like this as given below

1 Ansicht (letzte 30 Tage)
RAJAN PRASAD
RAJAN PRASAD am 5 Aug. 2016
Bearbeitet: Stephen23 am 6 Aug. 2016
0 0 3 4 5 0 0 0 0 10 11 12 13 14 15 0 0 0 0 20 21 22 0 0 0 is the data in which i have to subtract (5-3),(15-10),(22-20) can anybody tell me how to do it
  2 Kommentare
Stephen23
Stephen23 am 5 Aug. 2016
@ RAJAN PRASAD: your question is not clear: do you want to sort the data (as your title states), or do you want to calculate differences between values (as your examples shows) ?
RAJAN PRASAD
RAJAN PRASAD am 6 Aug. 2016
I wanted to subtract only it helped

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 5 Aug. 2016
Bearbeitet: Stephen23 am 6 Aug. 2016
This code locates the beginning and end values of any non-zero sequence, and calculates their differences:
>> X = [0,0,3,4,5,0,0,0,0,10,11,12,13,14,15,0,0,0,0,20,21,22,0,0,0];
>> Y = diff([0,X,0]>0);
>> X(Y(2:end)<0) - X(Y(1:end-1)>0)
ans =
2 5 2
Have a look at X(Y(2:end)<0) and X(Y(1:end-1)>0) if you want to know how this works.

Weitere Antworten (0)

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by