Add vectors in loop
조회 수: 2 (최근 30일)
이전 댓글 표시
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
Allen
2021년 3월 1일
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?
채택된 답변
Allen
2021년 3월 2일
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
추가 답변 (1개)
darova
2021년 3월 2일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!