Add vectors in loop

1 次查看(过去 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
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
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
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 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by