Add vectors in loop

조회 수: 2 (최근 30일)
Jørgen Fone Pedersen
Jørgen Fone Pedersen 2021년 3월 1일
댓글: Jørgen Fone Pedersen 2021년 3월 3일
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
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?
Jørgen Fone Pedersen
Jørgen Fone Pedersen 2021년 3월 1일
편집: Jørgen Fone Pedersen 2021년 3월 1일
Yes, sorry about that. Tried adding the .mat file but its too large.
The Timeseries_norm is just a normalized signal sampled at 1 Mhz. The ipts_shift is the indexes for start and stop in the timeseries for certain events i want to extract.
I multiply indices with timeseries later to zero out all points before index 1 and after index 2, before index 3 and after index 4 etc..
>> whos TimeSeries_norm ipts_shift
Name Size Bytes Class Attributes
TimeSeries_norm 80000000x1 320000000 single
ipts_shift 1x20 160 double
>>

댓글을 달려면 로그인하십시오.

채택된 답변

Allen
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
Jørgen Fone Pedersen
Jørgen Fone Pedersen 2021년 3월 3일
Thank you.
This works perfectly and looks so much better.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

darova
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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by