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.

 채택된 답변

Luca Amerio
Luca Amerio 2015년 5월 26일

0 개 추천

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

Stephen23
Stephen23 2015년 5월 26일
편집: Stephen23 2015년 5월 26일
Or a little simpler:
P(P~=1:length(P))
E.g.:
>> P = [1,2,9,4,5,6,1,8,9];
>> P(P~=1:length(P))
ans =
9 1
Thank you sir, but for 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
your code giving error like >> P(P~=1:length(P)) Error using ~= Matrix dimensions must agree.
i have checked that your code P(~(P==1:length(P))) Works if P is a row vector.
what changes i should incorporate in above code to work if P is column vector. Am not interested to convert row to column and vice-versa.
P(P(:)'~=1:length(P))
Thank you sir

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2015년 5월 26일

0 개 추천

out = P([1;diff(P)] < 0);

댓글 수: 5

Thank you but for >> P = [1,2,9,4,5,6,1,8,9]; Am getting error like >> out = P([1;diff(P)] < 0) Error using vertcat Dimensions of matrices being concatenated are not consistent.
Thorsten
Thorsten 2015년 5월 26일
편집: Thorsten 2015년 5월 26일
You have to convert P to a column vector
P = P(:);
out = P([1;diff(P)] < 0);
or use "," instead of ";", but this works only if P is a row vector
out = P([1,diff(P)] < 0);
Raghavendra Reddy P
Raghavendra Reddy P 2015년 5월 26일
편집: Andrei Bobrov 2015년 5월 26일
for P = [1,2,9,4,5,6,1,8,9] if i use code out = P([1,diff(P)] < 0) am getting out = [ 4 1] instead of [9 1]
P(strfind([P(1);diff(P(:))]' ~= 1,[1 1]))
Thank you

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by