logical to index values convert

7 次查看(过去 30 天)
Megha
Megha 2019-3-3
编辑: Stephan 2019-3-3
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?

回答(1 个)

Stephan
Stephan 2019-3-3
编辑:Stephan 2019-3-3
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

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by