logical to index values convert

3 vues (au cours des 30 derniers jours)
Megha
Megha le 3 Mar 2019
Modifié(e) : Stephan le 3 Mar 2019
I have a set of values, like a = [1 2 3 4 5 6 7 6 5 4 3 2 1]; % 1x13 double
Now, i wish to get the index of value for which numbers are increasing and decreasing.
For this i used,
for i = 1:length(a)-1
out = find(a(i)>a(i+1));
in = find(a(i)<a(i+1));
end
however, it gives me error.
so i tried,
for i = 1:length(a)-1
out = (a(i)>a(i+1));
in = (a(i)<a(i+1));
end
Now, I have logical o/p. How to fetch index numbers from this logical o/p?

Réponses (1)

Stephan
Stephan le 3 Mar 2019
Modifié(e) : Stephan le 3 Mar 2019
Hi,
try:
a = [1 2 3 4 5 6 7 6 5 4 3 2 1]; % 1x13 double
for i = 1:length(a)-1
out(i) = (a(i)>a(i+1));
in(i) = (a(i)<a(i+1));
end
in = find(in)
out = find(out)
Note that you can get the same reult without a loop:
in_easy = find(diff(a)>0)
out_easy = find(diff(a)<0)
Best regards
Stephan

Catégories

En savoir plus sur Matrices and Arrays 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!

Translated by