How to assign variables based on index position
20 views (last 30 days)
Show older comments
I want to assign a variable based on index position
a = [1 1 1 1 1.6 1.1 1.1 1.6 1.1 1.1]
find(a > 1.5)
This gives me the position of those values in the array
ans =
5 8
I then want to assign them a variable such that gap1 = 5, gap2 = 8 to gap(n) = #
Following this, I want to apply these variables
getNPVI(a(1:gap1 - 1))
getNPVI(a(gap1 + 1:gap2 - 1))
until getNPVI(a(lastgap + 1:end))
Any guidance is appreciated, thank you.
0 Comments
Answers (2)
Samuel Londner
on 1 Dec 2019
Edited: Samuel Londner
on 1 Dec 2019
For the assignment problem I would try something like that:
kk = a > 1.5;
indexes = 1:length(a);
out = a*(~kk) + indexes * kk;
For your second problem try using a Hankel matrix as an index matrix. A simple "for" loop would also do the trick.
0 Comments
KALYAN ACHARJYA
on 1 Dec 2019
Edited: KALYAN ACHARJYA
on 1 Dec 2019
One way:
a = [1 1 1 1 1.6 1.1 1.1 1.6 1.1 1.1]
result=find(a > 1.5)
Now access the result data as result(1) and result(2) ..array indexing is recomended rather than different variable names
0 Comments
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!