필터 지우기
필터 지우기

Compare segments of a vector

조회 수: 1 (최근 30일)
Ricardo Gutierrez
Ricardo Gutierrez 2018년 3월 6일
편집: Fangjun Jiang 2018년 3월 7일
Hello good day.
I have a problem, which I hope you can give me support or advice: I have the following column vector of 20 elements (the real one is 1000 elements): A=[6.9563; 6.9563; 6.9563; 6.9563; 6.9563; 7.4917; 5.8343; 5.3075; 5.8713; 7.1312; 11.2175; 11.2175; 11.2175; 11.2175; 11.2175; 10.4278; 10.9766; 11.2685; 9.4748; 11.3789] The question is how to compare (greater than>) the first 5 elements with the second 5 elements of vector A. 6.9563 6.9563 6.9563 6.9563 6.9563 Compare with 7.4917 5.8343 5.3075 5.8713 7.1312 and after third 5 elements with the fourth 5 elements of the vector A 11.2175 11.2175 11.2175 11.2175 11.2175 Compare with 10.4278 10.9766 11.2685 9.4748 11.3789 And so on automatically, if the vector A is 1000 elements
I hope you understand me and help me. Greetings.
  댓글 수: 5
Jos (10584)
Jos (10584) 2018년 3월 6일
I fail see the relationship between the values of B and the values of A ...
Ricardo Gutierrez
Ricardo Gutierrez 2018년 3월 7일
You're right I put them back A=[2 2 2 2 2 3 6 3 4 5 1 1 1 1 1 2 3 4 5 6]........ 2 2 2 2 2 3 6 3 4 5 in the second 5 values; at least one value is greater than 2 therefore the values pass:3 6 3 4 5.....in the following values 1 1 1 1 1 2 3 4 5 6 the last 5 values at least one value is greater than 1 therefore the values pass:2 3 4 5 6....so it would be in vector B....B=[3 6 3 4 5 2 3 4 5 6 ]

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

채택된 답변

Fangjun Jiang
Fangjun Jiang 2018년 3월 7일
편집: Fangjun Jiang 2018년 3월 7일
What if none of the element in second 5 elements is greater than the first 5 elements? The code below might be easier to understand.
A=[2 2 2 2 2 3 6 3 4 5 1 1 1 1 1 0 0 0 0 0];
A=transpose(reshape(A,10,[]));
B=A(:,6:10);
A=A(:,1:5);
C=B>A;
ind=any(C,2);
B(~ind,:)=A(~ind,:)
B =
3 6 3 4 5
1 1 1 1 1

추가 답변 (3개)

David Fletcher
David Fletcher 2018년 3월 6일
편집: David Fletcher 2018년 3월 6일
ind=repmat(logical([0 0 0 0 0 1 1 1 1 1]),1,100)
b=a(ind)
a(ind)=[]
c=a>b

Fangjun Jiang
Fangjun Jiang 2018년 3월 6일
This example should give you an idea
A=transpose(reshape(rand(20,1),5,[]))
B=diff(A)
C=B>0
C should tell you the relation

Jos (10584)
Jos (10584) 2018년 3월 7일
편집: Jos (10584) 2018년 3월 7일
A = [2 2 2 2 2 3 6 3 4 5 1 1 1 1 1 2 3 4 5 6]
AA = reshape(A,5,[])
dA = diff(AA,[],2)
ix = all(dA>0)
ix = 2*find(ix(1:2:end))
B = reshape(AA(:,ix),1,[])
  댓글 수: 2
Ricardo Gutierrez
Ricardo Gutierrez 2018년 3월 7일
Your code shows as a result:..B = 2 2 2 2 2 1 1 1 1 1...and must be:...B = 3 6 3 4 5 2 3 4 5 6....thank you very much for your work and time
Jos (10584)
Jos (10584) 2018년 3월 7일
Oh, I subtracted a 1 from ix by mistake! Corrected

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by