finding Missing elements in a vector

1 vue (au cours des 30 derniers jours)
Raghavendra Reddy P
Raghavendra Reddy P le 26 Mai 2015
I have a vector P =
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
19
20
21
3
23
24
6
26
27
28
29
30
31
32
As per the ascending order (1 2...15..32) of vector P, the elements 18,25,28 are missing and their position is occupied by the elements 2,3,6. now i want a vector which indicates this numbers like n=[2 3 6]. if elements are in order perfectly (1:10), I don't want new vector n.

Réponse acceptée

Luca Amerio
Luca Amerio le 26 Mai 2015
This will do the trick
P(~(P==1:length(P)))
just for clarification
P==1:length(P)
returns a logical array of the position occupied by the right number.
~(P==1:length(P))
is the logical array of the position occupied by the WRONG number
P(~(P==1:length(P)))
get the values in those positions.
  5 commentaires
Andrei Bobrov
Andrei Bobrov le 26 Mai 2015
P(P(:)'~=1:length(P))
Raghavendra Reddy P
Raghavendra Reddy P le 26 Mai 2015
Thank you sir

Connectez-vous pour commenter.

Plus de réponses (1)

Andrei Bobrov
Andrei Bobrov le 26 Mai 2015
out = P([1;diff(P)] < 0);
  5 commentaires
Andrei Bobrov
Andrei Bobrov le 26 Mai 2015
P(strfind([P(1);diff(P(:))]' ~= 1,[1 1]))
Raghavendra Reddy P
Raghavendra Reddy P le 26 Mai 2015
Thank you

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by