Add vectors in loop
2 views (last 30 days)
Show older comments
Jørgen Fone Pedersen
on 1 Mar 2021
Commented: Jørgen Fone Pedersen
on 3 Mar 2021
How would one do something like this simpler? It works but i feel terrible for doing it this way and it takes up heaps of memory i guess
t_er = linspace(1,length(TimeSeries_norm),length(TimeSeries_norm));
ind1 = t_er >= ipts_shift(1) & t_er < ipts_shift(2);
ind2 = t_er >= ipts_shift(3) & t_er < ipts_shift(4);
ind3 = t_er >= ipts_shift(5) & t_er < ipts_shift(6);
ind4 = t_er >= ipts_shift(7) & t_er < ipts_shift(8);
ind5 = t_er >= ipts_shift(9) & t_er < ipts_shift(10);
ind6 = t_er >= ipts_shift(11) & t_er < ipts_shift(12);
ind7 = t_er >= ipts_shift(13) & t_er < ipts_shift(14);
ind8 = t_er >= ipts_shift(15) & t_er < ipts_shift(16);
ind9 = t_er >= ipts_shift(17) & t_er < ipts_shift(18);
ind10 = t_er >= ipts_shift(19) & t_er < ipts_shift(20);
indices = ind1+ind2+ind3+ind4+ind5+ind6+ind7+ind8+ind9+ind10;
TR_er = TimeSeries_norm' .* indices;
2 Comments
Allen
on 1 Mar 2021
Jorgen,
Can you provide an example of the TimeSeries_norm and ipts_shift variables you are working with or preferably at least array dimensions/sizes?
Accepted Answer
Allen
on 2 Mar 2021
I am certain that there is a much better method using vectorized indexing, but without seeing an example of the values in TimeSeries_norm and ipts_shift, then a for-loop can get the job done fairly efficiently. @darova's method will work in generating an equivalent result for your indices variable, but needs a few corrections to the code in order to do so. Also, be aware that this method assumes that ipts_shift uses an even number of values, and that they are all paired values representing start/stop events.
indices = 0;
for i = 1:2:length(ipts_shift)
indices = indices + (ipts_shift(i)<=t_er & t_er<ipts_shift(i+1));
end
More Answers (1)
darova
on 2 Mar 2021
Maybe for loop
ind = 0;
for i = 1:2:length(ipts_shift)
ind1 = ipts_shift(1) <= t_er & t_er < ipts_shift(2);
ind = ind + ind1;
end
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!