Comparing elements of a vector.
Mostra commenti meno recenti
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 Commenti
Shubham Gupta
il 10 Ott 2019
Modificato: Shubham Gupta
il 10 Ott 2019
I am getting the output that you needed, using the code that you have provided. I don't see any problem.
T = [1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21];
R = [1;2;3;4;5;6;7;8;9;10;10;10;11;12;12;12;12;12;13;13;13];
g = [1,1];
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
Output is
g =
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
13 11
14 12
19 13
Am I misunderstanding something?
Madhurima Reddy
il 10 Ott 2019
Shubham Gupta
il 10 Ott 2019
I am glad I could help. Also, you might wanna look at @the_cyclist 's answer for better performance.
Risposte (1)
the cyclist
il 10 Ott 2019
[g(:,2), idx] = unique(R);
g(:,1) = T(idx);
3 Commenti
Shubham Gupta
il 10 Ott 2019
There is one problem with that, when data decreases at some point user don't want to take that point but unique will take that data into the account. For e.g.
R = [1,2,3,4,5,7,6,8,10,10,10,11,11,11];
T = [1,2,3,4,5,6,7,8,9,10,11,12,13,14];
[g(:,2), idx] = unique(R);
g(:,1) = T(idx);
will output
g =
1 1
2 2
3 3
4 4
5 5
7 6 % user wouldn't want this data
6 7
8 8
9 10
12 11
That being said, if data is monotically increasing or there is always unit difference between current and successive value then it should be no problem at all.
the cyclist
il 10 Ott 2019
You are correct.
But the original question explicitly stated that R is increasing, so I assumed that that was OK.
Madhurima Reddy
il 10 Ott 2019
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!