Comparing elements of a vector.
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Shubham Gupta
le 10 Oct 2019
I am glad I could help. Also, you might wanna look at @the_cyclist 's answer for better performance.
Réponses (1)
the cyclist
le 10 Oct 2019
[g(:,2), idx] = unique(R);
g(:,1) = T(idx);
3 commentaires
the cyclist
le 10 Oct 2019
You are correct.
But the original question explicitly stated that R is increasing, so I assumed that that was OK.
Voir également
Catégories
En savoir plus sur Introduction to Installation and Licensing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!