Comparing elements of a vector.

조회 수: 2 (최근 30일)
Madhurima Reddy
Madhurima Reddy 2019년 10월 10일
댓글: Madhurima Reddy 2019년 10월 10일
I have two vectors named R and T, where elements in R keep on increasing and finally reaches a steady state value.
Let's say T = [1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21] and R = [1;2;3;4;5;6;7;8;9;10;10;10;11;12;12;12;12;12;13;13;13].
I intend to get g(:,1) = [1;2;3;4;5;6;7;8;9;10;13;14;19] and g(:,2) = [1;2;3;4;5;6;7;8;9;10;11;12;13]. But I am getting all the values in g. Could you please help me out with this.
Thanks and Regards.
L = length(R);
for h = 1:1:L-1
dffrad = R(h+1)-R(h);
dfftime= T(h+1)-T(h);
if (dffrad~=0 && dffrad>0)
g = [g; T(h+1) R(h+1)];
end
end
  댓글 수: 3
Madhurima Reddy
Madhurima Reddy 2019년 10월 10일
The values in my data(not that's given in the question) were getting rounded off to nearest decimal and that's why I thought the values were repeating. Thank you Shubham for your help.
Shubham Gupta
Shubham Gupta 2019년 10월 10일
I am glad I could help. Also, you might wanna look at @the_cyclist 's answer for better performance.

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

답변 (1개)

the cyclist
the cyclist 2019년 10월 10일
[g(:,2), idx] = unique(R);
g(:,1) = T(idx);
  댓글 수: 3
the cyclist
the cyclist 2019년 10월 10일
You are correct.
But the original question explicitly stated that R is increasing, so I assumed that that was OK.
Madhurima Reddy
Madhurima Reddy 2019년 10월 10일
My data does decrease after reaching a max value. However, Thank you to both of you for help.

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

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by