logical to index values convert

4 Ansichten (letzte 30 Tage)
Megha
Megha am 3 Mär. 2019
Bearbeitet: Stephan am 3 Mär. 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?

Antworten (1)

Stephan
Stephan am 3 Mär. 2019
Bearbeitet: Stephan am 3 Mär. 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

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by